/// <summary> /// Executes all the EF data context migrations against the configured database /// </summary> private void EnsureMigrations(DbTestRunnerConfiguration config, IDbTestRunnerContext context, DbInitializationResult result) { try { using (var scope = context.ServiceProvider.CreateScope()) { var migrationRunner = scope.ServiceProvider.GetService <DataMigrationRunner>(); migrationRunner.ApplyMigrations(config.DataContextType); result.Logs.Add("DB Migrations completed."); result.IsSuccessful = true; } } catch (Exception e) { result.Logs.Add($"Encountered exception running migrations. Exception: {e.Message} Details: {e}"); throw; } }
public void RegisterDataContext(IServiceCollection services, IDbTestRunnerContext context, DbTestRunnerConfiguration config, IEfDbScaffolder scaffolder) { scaffolder.RegisterServices <TDataContext>(services, context, config); }
// // EFCORE // protected override IDbTestRunnerConfiguration ProduceDefaultConfiguration(IConfiguration systemConfig) { var config = DbTestRunnerConfiguration.ProduceDefaultConfiguration <TDataContext>(systemConfig); return(config); }
/// <summary> /// Configures all the services necessary to produce the data context from the IoC container. /// </summary> private void ConfigureDataContext <TDataContext>(IServiceCollection services, IDbTestRunnerContext context, DbTestRunnerConfiguration config) where TDataContext : DbContext { // Our data contexts require loggers, so add the loggers to the services collection services.AddLogging(); // if a db name is specified, use that, otherwise generate a unique database name context.DbName = context.DbName ?? config.DbName ?? ProduceUniqueDatabaseName <TDataContext>(); // build an ado.net connection string for the server and database configured. // turn off connection pooling to prevent it from holding on to the connection after we're done using it. context.ConnectionString = ProduceConnectionString(config, context.DbName, useConnectionPooling: false); var connectionString = context.ConnectionString; services.AddDbContext <TDataContext>(options => { options.UseSqlServer(connectionString); }); // register the data migration runner. services.AddDataMigrationRunner(); }
/// <summary> /// Participates in the registration of services /// during DbTestRunner startup. /// /// Provides an opportunity to inject data related /// services into the IoC container. /// </summary> public void RegisterServices <TDataContext>(IServiceCollection services, IDbTestRunnerContext context, DbTestRunnerConfiguration config) where TDataContext : DbContext { // produce and register the data context. ConfigureDataContext <TDataContext>(services, context, config); }