예제 #1
0
        /// <summary>
        /// 初始化 <see cref="DefaultContextService"/> 类的新实例。
        /// </summary>
        /// <param name="context"></param>
        /// <param name="databaseFactory">一个用于创建 <see cref="IDatabase"/> 的工厂函数。</param>
        public DefaultContextService(EntityContextInitializeContext context, Func <IProvider, ConnectionString, IDatabase> databaseFactory)
            : base(context)
        {
            var options = context.Options;

            Func <IDatabase> factory = null;

            if (DatabaseScope.Current != null)
            {
                factory = () => DatabaseScope.Current.Database;
            }
            else if (databaseFactory != null)
            {
                factory = () => databaseFactory(context.Provider, context.ConnectionString);
            }
            else if (context.Provider != null && context.ConnectionString != null)
            {
                factory = () => new Database(context.ConnectionString, context.Provider);
            }
            else if (options != null)
            {
                factory = () => DatabaseFactory.CreateDatabase(options.ConfigName);
            }

            if (factory != null)
            {
                Database = EntityDatabaseFactory.CreateDatabase(InstanceName, factory);
                Provider = Database.Provider;
            }
        }
예제 #2
0
        /// <summary>
        /// 初始化 <see cref="EntityTransactionScope"/> 类的新实例。
        /// </summary>
        /// <param name="option">选项。</param>
        public EntityTransactionScope(EntityTransactionScopeOption option = null)
        {
            var database = DatabaseFactory.GetDatabaseFromScope();

            if (database != null)
            {
                SetDatabase(DatabaseScope.Current.InstanceName, database);
                EntityDatabaseFactory.StartTransaction(database, option);
            }

            Option = option;
        }