Exemplo n.º 1
0
 public DbContext GetDbContext()
 {
     //是否存在工作单元
     if (_uowHelper.IsExistUow)
     {
         var key = dbContextType.FullName;
         if (_uowHelper.IsExistConnectionKey(key))
         {
             var efuowConnection = (EFUOWConnection)_uowHelper.GetOrAddUOWConnection(key, null);
             return(efuowConnection.dbContext);
         }
         var dbcontext     = (DbContext)_serviceProvider.GetRequiredService(dbContextType);
         var dbtransaction = dbcontext.Database.BeginTransaction();
         _uowHelper.GetOrAddUOWConnection(key, new EFUOWConnection {
             dbContext = dbcontext, DbContextTransaction = dbtransaction
         });
         return(dbcontext);
     }
     else
     {
         return((DbContext)_serviceProvider.GetRequiredService(dbContextType));
     }
 }
Exemplo n.º 2
0
 public ICafMongoDbContext GetDbContext()
 {
     //是否存在工作单元
     if (_uowHelper.IsExistUow)
     {
         var key = dbContextType.FullName;
         if (_uowHelper.IsExistConnectionKey(key))
         {
             var mongouowConnection = (MongoDbUOWConnection)_uowHelper.GetOrAddUOWConnection(key, null);
             return((ICafMongoDbContext)mongouowConnection.DbContext);
         }
         var dbcontext     = (ICafMongoDbContext)_serviceProvider.GetRequiredService(dbContextType);
         var dbtransaction = dbcontext.mongoClient.StartSession();
         _uowHelper.GetOrAddUOWConnection(key, new MongoDbUOWConnection {
             DbContext = dbcontext, ClientSession = dbtransaction
         });
         return((ICafMongoDbContext)dbcontext);
     }
     else
     {
         return((ICafMongoDbContext)_serviceProvider.GetRequiredService(dbContextType));
     }
 }