public override void Initialize(IServiceProvider serviceProvider, WebshopContext context) { base.Initialize(serviceProvider, context); // check if database is healthy if (!DbIsHealthy(context)) { base.RecreateDatabase(context); } else { return; } // seed data var productCategories = ProductCategorySeeder.GetSeedList(); context.ProductCategories.AddRange(productCategories); context.SaveChanges(); var addresses = AddressSeeder.GetSeedList(); context.Addresses.AddRange(addresses); context.SaveChanges(); var customers = CustomerSeeder.GetSeedList(); context.Customers.AddRange(customers); context.SaveChanges(); var images = ImageSeeder.GetSeedList(); context.Images.AddRange(images); context.SaveChanges(); var warehouses = WarehouseSeeder.GetSeedList(); context.Warehouses.AddRange(warehouses); context.SaveChanges(); var products = ProductSeeder.GetSeedList(); context.Products.AddRange(products); context.SaveChanges(); var orders = OrderSeeder.GetSeedList(); context.Orders.AddRange(orders); context.SaveChanges(); var orderProducts = OrderProductSeeder.GetSeedList(); context.OrderProducts.AddRange(orderProducts); context.SaveChanges(); }
public bool DbIsHealthy(WebshopContext context) { return(context.ProductCategories.Count() == ProductCategorySeeder.Count() && context.Addresses.Count() == AddressSeeder.Count() && context.Customers.Count() == CustomerSeeder.Count() && context.Images.Count() == ImageSeeder.Count() && context.Warehouses.Count() == WarehouseSeeder.Count() && context.Products.Count() == ProductSeeder.Count() && context.Orders.Count() == OrderSeeder.Count() && context.OrderProducts.Count() == OrderProductSeeder.Count()); }
private static void Build() { if (_orderProducts.Any()) { return; } var productsCount = ProductSeeder.Count(); var ordersCount = OrderSeeder.Count(); var amounts = new[] { 1, 2, 3, 5, 10 }; var prices = new[] { 5, 10, 15, 20, 25, 30, 40, 50 }; var orderProducts = new List <OrderProduct>(); for (var i = 1; i <= ordersCount; i++) { var offset = i % 2 == 0 ? (productsCount - i) % 7 : 0; var numberOfProducts = ((i + offset) % 5 * 4); for (var j = 1; j <= numberOfProducts; j++) { var price = prices[(i + j) % prices.Length]; var amount = amounts[(i + j + offset) % amounts.Length]; var productId = (price * amount * i * j * 7) % productsCount; productId = productId == 0 ? 5 : productId; if (orderProducts.Any(x => x.ProductId == productId && x.OrderId == i)) { continue; } orderProducts.Add(new OrderProduct() { ProductId = productId, Amount = amount, OrderId = i, Price = price }); } } _orderProducts = orderProducts; }