// // Summary: // returns a WebApplicationFactory without mocked services // private static WebApplicationFactory <Startup> GetHostBuilder(WebApplicationFactory <Todo_App.Startup> factory) { return(factory .WithWebHostBuilder(builder => { builder.ConfigureTestServices(services => { var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions <IdDbContext>)); services.Remove(descriptor); services.AddDbContext <IdDbContext>(options => { options.UseSqlite(_connection); }); var sp = services.BuildServiceProvider(); using (var scope = sp.CreateScope()) { var scopedServices = scope.ServiceProvider; var logger = scopedServices .GetRequiredService <ILogger <UserControllerTests> >(); try { var context = scopedServices .GetRequiredService <IdDbContext>(); var roleManager = scopedServices .GetRequiredService <RoleManager <Role> >(); var configurationDbContext = scopedServices.GetRequiredService <ConfigurationDbContext>(); DataSeedingContext.Initialize(context, roleManager, configurationDbContext); } catch (Exception ex) { logger.LogError(ex, "An Error ocurred seeding the" + "database with test messages. Error: {Message}", ex.Message); } } }); })); }
public static void CreateDbIfNotExists(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <IdDbContext>(); var roleManager = services.GetRequiredService <RoleManager <Role> >(); var configurationDbContext = services.GetRequiredService <ConfigurationDbContext>(); var persistantGrantDbContext = services.GetRequiredService <PersistedGrantDbContext>(); // Add more services here if required when seeding the database DataSeedingContext.Initialize(context, roleManager, configurationDbContext, persistantGrantDbContext); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating the DB."); } } }
static void Main(string[] args) { using (var context = new DataSeedingContext()) context.Database.EnsureCreated(); }