Esempio n. 1
0
        public async Task <TDbContext> GetDbContextAsync()
        {
            var unitOfWork = _unitOfWorkManager.Current;

            if (unitOfWork == null)
            {
                throw new AbpException("A DbContext can only be created inside a unit of work!");
            }

            var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>();
            var connectionString     = await _connectionStringResolver.ResolveAsync(connectionStringName);

            var dbContextKey = $"{typeof(TDbContext).FullName}_{connectionString}";

            var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);

            if (databaseApi == null)
            {
                databaseApi = new EfCoreDatabaseApi <TDbContext>(
                    await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString)
                    );

                unitOfWork.AddDatabaseApi(dbContextKey, databaseApi);
            }

            return(((EfCoreDatabaseApi <TDbContext>)databaseApi).DbContext);
        }
Esempio n. 2
0
        public async Task <TDbContext> GetDbContextAsync()
        {
            var unitOfWork = _unitOfWorkManager.Current;

            if (unitOfWork == null)
            {
                throw new AbpException("A DbContext can only be created inside a unit of work!");
            }

            var targetDbContextType  = _options.GetReplacedTypeOrSelf(typeof(TDbContext));
            var connectionStringName = ConnectionStringNameAttribute.GetConnStringName(targetDbContextType);
            var connectionString     = await ResolveConnectionStringAsync(connectionStringName);

            var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}";

            var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);

            if (databaseApi == null)
            {
                databaseApi = new EfCoreDatabaseApi(
                    await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString)
                    );

                unitOfWork.AddDatabaseApi(dbContextKey, databaseApi);
            }

            return((TDbContext)((EfCoreDatabaseApi)databaseApi).DbContext);
        }