private static DbContextCreationContext GetCreationContext <TDbContext>(IServiceProvider serviceProvider)
            where TDbContext : EPTDbContext <TDbContext>
        {
            var context = DbContextCreationContext.Current;

            if (context != null)
            {
                return(context);
            }

            var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>();
            var connectionString     = serviceProvider.GetRequiredService <IConnectionStringResolver>().Resolve(connectionStringName);

            return(new DbContextCreationContext(
                       connectionStringName,
                       connectionString
                       ));
        }
Exemple #2
0
        public TDbContext GetDbContext()
        {
            var unitOfWork = _unitOfWorkManager.Current;

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

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

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

            var databaseApi = unitOfWork.GetOrAddDatabaseApi(
                dbContextKey,
                () => new EfCoreDatabaseApi <TDbContext>(
                    CreateDbContext(unitOfWork, connectionStringName, connectionString)
                    ));

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