/// <summary> /// 获取指定数据上下文类型<typeparamref name="TEntity"/>的实例 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <typeparam name="TKey">实体主键类型</typeparam> /// <returns><typeparamref name="TEntity"/>所属上下文类的实例</returns> public IDbContext GetDbContext <TEntity, TKey>() where TEntity : IEntity <TKey> where TKey : IEquatable <TKey> { IEntityConfigurationTypeFinder typeFinder = _serviceProvider.GetService <IEntityConfigurationTypeFinder>(); Type entityType = typeof(TEntity); Type dbContextType = typeFinder.GetDbContextTypeForEntity(entityType); DbContext dbContext; OSharpDbContextOptions dbContextOptions = GetDbContextResolveOptions(dbContextType); DbContextResolveOptions resolveOptions = new DbContextResolveOptions(dbContextOptions); IDbContextResolver contextResolver = _serviceProvider.GetService <IDbContextResolver>(); ActiveTransactionInfo transInfo = ActiveTransactionInfos.GetOrDefault(resolveOptions.ConnectionString); //连接字符串的事务不存在,添加起始上下文事务信息 if (transInfo == null) { resolveOptions.ExistingConnection = null; dbContext = contextResolver.Resolve(resolveOptions); RelationalDatabaseCreator dbCreator; if ((dbCreator = dbContext.GetService <IDatabaseCreator>() as RelationalDatabaseCreator) != null) { if (!dbCreator.Exists()) { throw new OsharpException($"数据上下文“{dbContext.GetType().FullName}”的数据库不存在,请通过 Migration 功能进行数据迁移创建数据库。"); } } IDbContextTransaction transaction = dbContext.Database.BeginTransaction(); transInfo = new ActiveTransactionInfo(transaction, dbContext); ActiveTransactionInfos[resolveOptions.ConnectionString] = transInfo; } else { resolveOptions.ExistingConnection = transInfo.DbContextTransaction.GetDbTransaction().Connection; //相同连接串相同上下文类型并且已存在对象,直接返回上下文对象 if (transInfo.StarterDbContext.GetType() == resolveOptions.DbContextType) { return(transInfo.StarterDbContext as IDbContext); } dbContext = contextResolver.Resolve(resolveOptions); if (dbContext.IsRelationalTransaction()) { dbContext.Database.UseTransaction(transInfo.DbContextTransaction.GetDbTransaction()); } else { dbContext.Database.BeginTransaction(); } transInfo.AttendedDbContexts.Add(dbContext); } return(dbContext as IDbContext); }
/// <summary> /// 获取指定数据上下文类型<typeparamref name="TEntity"/>的实例 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <typeparam name="TKey">实体主键类型</typeparam> /// <returns><typeparamref name="TEntity"/>所属上下文类的实例</returns> public IDbContext GetDbContext <TEntity, TKey>() where TEntity : IEntity <TKey> where TKey : IEquatable <TKey> { IEntityConfigurationTypeFinder typeFinder = _serviceProvider.GetService <IEntityConfigurationTypeFinder>(); Type dbContextType = typeFinder.GetDbContextTypeForEntity(typeof(TEntity)); DbContext dbContext; OsharpDbContextConfig dbContextConfig = GetDbContextResolveOptionsConfig(dbContextType); DbContextResolveOptions resolveOptions = new DbContextResolveOptions(dbContextConfig); IDbContextResolver contextResolver = _serviceProvider.GetService <IDbContextResolver>(); ActiveTransactionInfo transInfo = ActiveTransactionInfos.GetOrDefault(resolveOptions.ConnectionString); //连接字符串的事务不存在,添加起始上下文事务信息 if (transInfo == null) { resolveOptions.ExistingConnection = null; dbContext = contextResolver.Resolve(resolveOptions); IDbContextTransaction transaction = dbContext.Database.BeginTransaction(); transInfo = new ActiveTransactionInfo(transaction, dbContext); ActiveTransactionInfos[resolveOptions.ConnectionString] = transInfo; } else { resolveOptions.ExistingConnection = transInfo.DbContextTransaction.GetDbTransaction().Connection; dbContext = contextResolver.Resolve(resolveOptions); if (dbContext.IsRelationalTransaction()) { dbContext.Database.UseTransaction(transInfo.DbContextTransaction.GetDbTransaction()); } else { dbContext.Database.BeginTransaction(); } transInfo.AttendedDbContexts.Add(dbContext); } return(dbContext as IDbContext); }