示例#1
0
        public static IServiceCollection AddEntityFrameworkForApprenticeCommitments(this IServiceCollection services, IConfiguration config)
        {
            return(services.AddScoped(p =>
            {
                var unitOfWorkContext = p.GetService <IUnitOfWorkContext>();
                var connectionFactory = p.GetService <IConnectionFactory>();
                var loggerFactory = p.GetService <ILoggerFactory>();

                ApprenticeCommitmentsDbContext dbContext;
                try
                {
                    var synchronizedStorageSession = unitOfWorkContext.Get <SynchronizedStorageSession>();
                    var sqlStorageSession = synchronizedStorageSession.GetSqlStorageSession();
                    var optionsBuilder = new DbContextOptionsBuilder <ApprenticeCommitmentsDbContext>()
                                         .UseDataStorage(connectionFactory, sqlStorageSession.Connection)
                                         .UseLocalSqlLogger(loggerFactory, config);
                    if (config.IsLocalAcceptanceOrDev())
                    {
                        optionsBuilder.EnableSensitiveDataLogging().UseLoggerFactory(loggerFactory);
                    }
                    dbContext = new ApprenticeCommitmentsDbContext(optionsBuilder.Options);
                    dbContext.Database.UseTransaction(sqlStorageSession.Transaction);
                }
                catch (KeyNotFoundException)
                {
                    var settings = p.GetService <IOptions <ApplicationSettings> >().Value;
                    var optionsBuilder = new DbContextOptionsBuilder <ApprenticeCommitmentsDbContext>()
                                         .UseDataStorage(connectionFactory, settings.DbConnectionString)
                                         .UseLocalSqlLogger(loggerFactory, config);
                    dbContext = new ApprenticeCommitmentsDbContext(optionsBuilder.Options);
                }

                return dbContext;
            }));
        }
        public void EnsureCreated(ApprenticeCommitmentsDbContext dbContext)
        {
            if (!dbContext.Database.CanConnect())
            {
                throw new Exception(
                          @"SQL Server Acceptance Test database not found.

To ensure that we are testing against the real database schema used in production, do not rely on EFCore to create the database.

Instead, manually deploy the database using the `SFA.DAS.ApprenticeCommitments.Database` project, targetting `SFA.DAS.ApprenticeCommitments.AcceptanceTests`");
            }
        }
 public void EnsureDeleted(ApprenticeCommitmentsDbContext dbContext)
 {
     dbContext.Database.EnsureDeleted();
 }
 internal void EnsureDeleted(ApprenticeCommitmentsDbContext dbContext)
 => factory.EnsureDeleted(dbContext);
 public void EnsureDeleted(ApprenticeCommitmentsDbContext dbContext)
 {
 }