public static void InitialiseDatabase(ProductRepositoryDbContext context) { // Perform any migrations and save the changes context.Database.Migrate(); context.SaveChanges(); }
private async Task InitialiseDatabase(IApplicationBuilder app, IHostingEnvironment environment) { using (IServiceScope scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { ProductRepositoryDbContext productRepositoryDbContext = scope.ServiceProvider.GetRequiredService <ProductRepositoryDbContext>(); DatabaseSeeding.InitialiseDatabase(productRepositoryDbContext); } }
public ProductRepositoryManagerTests() { ProductRepositoryDbContextResolver = new Mock <Func <ProductRepositoryDbContext> >(); var builder = new DbContextOptionsBuilder <ProductRepositoryDbContext>().UseInMemoryDatabase($"Product-{Guid.NewGuid()}"); ProductRepositoryDbContext = ContextResolver(builder.Options); // Add data to the in memory db ProductRepositoryDbContext.Category.AddRange(ProductTestData.DBCategoryList); ProductRepositoryDbContext.Product.AddRange(ProductTestData.DBProductList); ProductRepositoryDbContext.SaveChanges(); Resolver = () => { return(new ProductRepositoryDbContext(builder.Options)); }; }
public void ProductRepositoryManager_GetCategories_NoProductsReturned() { // Create an empty db ProductRepositoryDbContextResolver = new Mock <Func <ProductRepositoryDbContext> >(); var builder = new DbContextOptionsBuilder <ProductRepositoryDbContext>().UseInMemoryDatabase($"Product-{Guid.NewGuid()}"); ProductRepositoryDbContext = ContextResolver(builder.Options); ProductRepositoryDbContext.SaveChanges(); Resolver = () => { return(new ProductRepositoryDbContext(builder.Options)); }; var productRepositoryManager = new ProductRepositoryManager(Resolver); CancellationToken cancellationToken = new CancellationToken(); Should.NotThrow(async() => { var result = await productRepositoryManager.GetCategories(cancellationToken); result.ShouldBeNull(); }); }