protected override void ConfigureDatabases(IServiceCollection services) { services.AddDbContext <AspNet5DataContext>(c => c.UseInMemoryDatabase("AspNet5WebApiTest").UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking), ServiceLifetime.Transient); using (var scope = services.BuildServiceProvider().CreateScope()) { var scopedServices = scope.ServiceProvider; var aspNet5DataContext = scopedServices.GetRequiredService <AspNet5DataContext>(); aspNet5DataContext.Database.EnsureCreated(); AspNet5DataSeed.SeedAsync(aspNet5DataContext, false).Wait(); } }
public ProductRepositoryTests(ITestOutputHelper output) { _output = output; var dbOptions = new DbContextOptionsBuilder <AspNet5DataContext>() .UseInMemoryDatabase(databaseName: "AspNet5RepositoryTest") .Options; _aspNet5DataContext = new AspNet5DataContext(dbOptions); AspNet5DataSeed.SeedAsync(_aspNet5DataContext, false).Wait(); _productRepository = new ProductRepository(_aspNet5DataContext); }
/// <summary> /// /// </summary> /// <param name="host"></param> private static void SeedData(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var logger = services.GetRequiredService <IAspNet5Logger <AspNet5DataSeed> >(); try { var aspNet5DataContext = services.GetRequiredService <AspNet5DataContext>(); AspNet5DataSeed.SeedAsync(aspNet5DataContext, true).Wait(); } catch (Exception ex) { logger.LogError("An error occurred seeding the DB.", ex); } } }