public virtual TDbContext GetOrCreateDbContext <TDbContext>() where TDbContext : DbContext { Type concreteDbContextType = _dbContextTypeMatcher.GetConcreteType(typeof(TDbContext)); var connectionStringResolveArgs = new ConnectionStringResolveArgs(); connectionStringResolveArgs["DbContextType"] = typeof(TDbContext); connectionStringResolveArgs["DbContextConcreteType"] = concreteDbContextType; string connectionString = ResolveConnectionString(connectionStringResolveArgs); string dbContextKey = concreteDbContextType.FullName + "#" + connectionString; DbContext dbContext; if (!ActiveDbContexts.TryGetValue(dbContextKey, out dbContext)) { if (Options.IsTransactional == true) { dbContext = _transactionStrategy.CreateDbContext <TDbContext>(connectionString, _dbContextResolver); } else { dbContext = _dbContextResolver.Resolve <TDbContext>(connectionString); } ((IObjectContextAdapter)dbContext).ObjectContext.ObjectMaterialized += (sender, args) => { ObjectContext_ObjectMaterialized(dbContext, args); }; FilterExecuter.As <IEfUnitOfWorkFilterExecuter>().ApplyCurrentFilters(this, dbContext); ActiveDbContexts[dbContextKey] = dbContext; } return((TDbContext)dbContext); }
/// <summary> /// 获取或创建数据库上下文 /// </summary> /// <typeparam name="TDbContext">数据库上下文对象</typeparam> /// <param name="multiTenancySide">多租户双方中的一方</param> /// <returns>数据库上下文对象</returns> public virtual TDbContext GetOrCreateDbContext <TDbContext>(MultiTenancySides?multiTenancySide = 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; DbContext dbContext; if (!ActiveDbContexts.TryGetValue(dbContextKey, out dbContext)) { dbContext = _dbContextResolver.Resolve <TDbContext>(connectionString); ((IObjectContextAdapter)dbContext).ObjectContext.ObjectMaterialized += (sender, args) => { ObjectContext_ObjectMaterialized(dbContext, args); }; FilterExecuter.As <IEfUnitOfWorkFilterExecuter>().ApplyCurrentFilters(this, dbContext); ActiveDbContexts[dbContextKey] = dbContext; } return((TDbContext)dbContext); }
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); } if (Options.Timeout.HasValue && !dbContext.Database.CommandTimeout.HasValue) { dbContext.Database.CommandTimeout = Options.Timeout.Value.TotalSeconds.To <int>(); } if (Clock.SupportsMultipleTimezone) { ((IObjectContextAdapter)dbContext).ObjectContext.ObjectMaterialized += (sender, args) => { ObjectContext_ObjectMaterialized(dbContext, args); }; } FilterExecuter.As <IEfUnitOfWorkFilterExecuter>().ApplyCurrentFilters(this, dbContext); ActiveDbContexts[dbContextKey] = dbContext; } return((TDbContext)dbContext); }
public virtual async Task <TDbContext> GetOrCreateDbContextAsync <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 = await ResolveConnectionStringAsync(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 = await _transactionStrategy .CreateDbContextAsync <TDbContext>(connectionString, _dbContextResolver); } else { dbContext = _dbContextResolver.Resolve <TDbContext>(connectionString); } if (dbContext is IShouldInitializeDcontext abpDbContext) { abpDbContext.Initialize(new AbpEfDbContextInitializationContext(this)); } FilterExecuter.As <IEfUnitOfWorkFilterExecuter>().ApplyCurrentFilters(this, dbContext); ActiveDbContexts[dbContextKey] = dbContext; return((TDbContext)dbContext); }
public virtual TDbContext GetOrCreateDbContext <TDbContext>() where TDbContext : DbContext { Type concreteDbContextType = _dbContextTypeMatcher.GetConcreteType(typeof(TDbContext)); var connectionStringResolveArgs = new ConnectionStringResolveArgs { ["DbContextType"] = typeof(TDbContext), ["DbContextConcreteType"] = concreteDbContextType }; string connectionString = ResolveConnectionString(connectionStringResolveArgs); string dbContextKey = concreteDbContextType.FullName + "#" + connectionString; if (!ActiveDbContexts.TryGetValue(dbContextKey, out DbContext dbContext)) { if (Options.IsTransactional == true) { dbContext = _transactionStrategy.CreateDbContext <TDbContext>(connectionString, _dbContextResolver); } else { dbContext = _dbContextResolver.Resolve <TDbContext>(connectionString); } if (Options.Timeout.HasValue && !dbContext.Database.CommandTimeout.HasValue) { dbContext.Database.CommandTimeout = Options.Timeout.Value.TotalSeconds.To <int>(); } if (Options.IsLazyLoadEnabled.HasValue) { dbContext.Configuration.LazyLoadingEnabled = Options.IsLazyLoadEnabled.Value; } ((IObjectContextAdapter)dbContext).ObjectContext.ObjectMaterialized += (sender, args) => { ObjectContext_ObjectMaterialized(dbContext, args); }; FilterExecuter.As <IEfUnitOfWorkFilterExecuter>().ApplyCurrentFilters(this, dbContext); ActiveDbContexts[dbContextKey] = dbContext; } return((TDbContext)dbContext); }
/// <summary> /// 获取dbContext /// </summary> /// <typeparam name="TDbContext"></typeparam> /// <returns></returns> public virtual TDbContext GetOrCreateDbContext <TDbContext>() where TDbContext : DbContext { //1:找到dbContext var concreteDbContextType = _dbContextTypeMatcher.GetConcreteType(typeof(TDbContext)); var connectionString = ResolveConnectionString(); var dbContextKey = concreteDbContextType.FullName + "#" + connectionString; DbContext dbContext; if (!ActiveDbContexts.TryGetValue(dbContextKey, out dbContext)) { if (DefaultOptions.IsTransactional == true) { dbContext = _transactionStrategy.CreateDbContext <TDbContext>(connectionString, _dbContextResolver); } else { dbContext = _dbContextResolver.Resolve <TDbContext>(connectionString); } if (DefaultOptions.Timeout.HasValue && !dbContext.Database.CommandTimeout.HasValue) { dbContext.Database.CommandTimeout = DefaultOptions.Timeout.Value.TotalSeconds.To <int>(); } ((IObjectContextAdapter)dbContext).ObjectContext.ObjectMaterialized += (sender, args) => { ObjectContext_ObjectMaterialized(dbContext, args); }; FilterExecuter.As <IEfUnitOfWorkFilterExecuter>().ApplyCurrentFilters(this, dbContext); ActiveDbContexts[dbContextKey] = dbContext; } return((TDbContext)dbContext); }