protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { var descriptor = services.SingleOrDefault( d => d.ServiceType == typeof(DbContextOptions <PokemonDbContext>) ); services.Remove(descriptor); services.AddDbContext <PokemonDbContext>(options => { options.UseInMemoryDatabase("InMemoryDbForTesting"); }); var sp = services.BuildServiceProvider(); using (var scope = sp.CreateScope()) { var scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService <PokemonDbContext>(); var logger = scopedServices .GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >(); db.Database.EnsureCreated(); try { DatabaseSeeder.InitializeDatabase(db); } catch (Exception ex) { logger.LogError(ex, "An error has occurred seeding the " + "database with data. Error: {Message}", ex.Message ); } } }); }