public virtual TDbContext GetOrCreateDbContext <TDbContext>(MultiTenancySides?multiTenancySide = null, string name = null)
            where TDbContext : DbContext
        {
            var concreteDbContextType = _dbContextTypeMatcher.GetConcreteType(typeof(TDbContext));

            var connectionStringResolveArgs = new ConnectionStringResolveArgs(multiTenancySide);

            connectionStringResolveArgs["DbContextType"]         = typeof(TDbContext);
            connectionStringResolveArgs["DbContextConcreteType"] = concreteDbContextType;
            var connectionString = ResolveConnectionString(connectionStringResolveArgs);

            var dbContextKey = concreteDbContextType.FullName + "#" + connectionString;

            if (name != null)
            {
                dbContextKey += "#" + name;
            }

            DbContext dbContext;

            if (!ActiveDbContexts.TryGetValue(dbContextKey, out dbContext))
            {
                if (Options.IsTransactional == true)
                {
                    dbContext = _transactionStrategy.CreateDbContext <TDbContext>(connectionString, _dbContextResolver);
                }
                else
                {
                    dbContext = _dbContextResolver.Resolve <TDbContext>(connectionString, null);
                }

                if (Options.Timeout.HasValue &&
                    dbContext.Database.IsRelational() &&
                    !dbContext.Database.GetCommandTimeout().HasValue)
                {
                    dbContext.Database.SetCommandTimeout(Options.Timeout.Value.TotalSeconds.To <int>());
                }

                //TODO: Object materialize event
                //TODO: Apply current filters to this dbcontext

                ActiveDbContexts[dbContextKey] = dbContext;
            }

            return((TDbContext)dbContext);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取或者创建数据库上下文
        /// </summary>
        /// <typeparam name="TDbContext">数据库上下文类型</typeparam>
        /// <param name="name">名称</param>
        /// <returns>数据库上下文对象</returns>
        public virtual TDbContext GetOrCreateDbContext <TDbContext>(string name = null)
            where TDbContext : DbContext
        {
            var dbContextType = typeof(TDbContext);

            // TODO: 这里获取连接字符串,有待开发
            var nameOrConnectionString = "Default";

            var dbContextKey = $"{dbContextType.FullName}#{nameOrConnectionString}";

            if (string.IsNullOrWhiteSpace(name))
            {
                dbContextKey = $"{dbContextKey}#{name}";
            }

            DbContext dbContext;

            if (!ActiveDbContexts.TryGetValue(dbContextKey, out dbContext))
            {
                if (Options.IsTransactional == true)
                {
                    dbContext = _transactionStrategy.CreateDbContext <TDbContext>(nameOrConnectionString, _dbContextResolver);
                }
                else
                {
                    dbContext = _dbContextResolver.Resolve <TDbContext>(nameOrConnectionString, null);
                }

                if (Options.Timeout.HasValue &&
                    dbContext.Database.IsRelational() &&
                    !dbContext.Database.GetCommandTimeout().HasValue)
                {
                    var commandTimeout = Convert.ToInt32(Options.Timeout.Value.TotalSeconds);

                    //dbContext.Database.SetCommandTimeout(Options.Timeout.Value.TotalSeconds.To<int>());
                    dbContext.Database.SetCommandTimeout(commandTimeout);
                }

                //TODO: Object materialize event
                //TODO: Apply current filters to this dbcontext

                ActiveDbContexts[dbContextKey] = dbContext;
            }

            return((TDbContext)dbContext);
        }
Esempio n. 3
0
        public virtual TDbContext GetOrCreateDbContext <TDbContext>(
            MultiTenancySides?multiTenancySide = null, string name = null)
            where TDbContext : DbContext
        {
            var concreteDbContextType = _dbContextTypeMatcher.GetConcreteType(typeof(TDbContext));

            var connectionStringResolveArgs = new ConnectionStringResolveArgs(multiTenancySide)
            {
                ["DbContextType"]         = typeof(TDbContext),
                ["DbContextConcreteType"] = concreteDbContextType
            };

            var connectionString = ResolveConnectionString(connectionStringResolveArgs);

            var dbContextKey = concreteDbContextType.FullName + "#" + connectionString;

            if (name != null)
            {
                dbContextKey += "#" + name;
            }

            if (ActiveDbContexts.TryGetValue(dbContextKey, out var dbContext))
            {
                return((TDbContext)dbContext);
            }

            if (Options.IsTransactional == true)
            {
                dbContext = _transactionStrategy
                            .CreateDbContext <TDbContext>(connectionString, _dbContextResolver);
            }
            else
            {
                dbContext = _dbContextResolver.Resolve <TDbContext>(connectionString, null);
            }

            if (dbContext is IShouldInitializeDcontext abpDbContext)
            {
                abpDbContext.Initialize(new AbpEfDbContextInitializationContext(this));
            }

            ActiveDbContexts[dbContextKey] = dbContext;

            return((TDbContext)dbContext);
        }
Esempio n. 4
0
        /// <summary>
        /// 获取或创建数据库上下文
        /// </summary>
        /// <param name="dbContextType">数据库上下文类型</param>
        /// <param name="name">名称</param>
        /// <returns>数据库上下文对象</returns>
        public virtual DbContext GetOrCreateDbContext()
        {
            // 获取连接字符串
            var nameOrConnectionString = _connectionStringResolver.Resolve(this.GetConnectionStringName());

            // 缓存键值
            var dbContextKey = this.GetDbContextKey(nameOrConnectionString);

            DbContext dbContext;

            if (!ActiveDbContexts.TryGetValue(dbContextKey, out dbContext))
            {
                if (Options.IsTransactional == true)
                {
                    dbContext = _transactionStrategy.CreateDbContext(nameOrConnectionString, _dbContextResolver, this._dbContextProviderName);
                }
                else
                {
                    dbContext = _dbContextResolver.Resolve(nameOrConnectionString, null, this.Options, this._dbContextProviderName);
                }

                if (Options.Timeout.HasValue &&
                    dbContext.Database.IsRelational() &&
                    !dbContext.Database.GetCommandTimeout().HasValue)
                {
                    var commandTimeout = Convert.ToInt32(Options.Timeout.Value.TotalSeconds);

                    //dbContext.Database.SetCommandTimeout(Options.Timeout.Value.TotalSeconds.To<int>());
                    dbContext.Database.SetCommandTimeout(commandTimeout);
                }

                //TODO: Object materialize event
                //TODO: Apply current filters to this dbcontext

                ActiveDbContexts[dbContextKey] = dbContext;
            }

            return(dbContext);
        }