public AddressControllerTest() { _dbOptions = new DbContextOptionsBuilder <AddressContext>() .UseInMemoryDatabase(databaseName: "in-memory") .Options; using (var dbContext = new AddressContext(_dbOptions)) { dbContext.AddRange(AddressContextDbSeed.GetData()); dbContext.SaveChanges(); } }
private static void CreateDbIfNotExists(IWebHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <AddressContext>(); AddressContextDbSeed.Seed(context); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating the DB."); } } }