public void SaveOrder(Order order) { context.AttachRange(order.Lines.Select(line => line.Product)); if (order.OrderId == 0) { context.Orders.Add(order); } context.SaveChanges(); }
private static void PopulateProducts(SportsStoreDbContext db) { if (db.Products.Any()) { return; } db.Products.AddRange( new Product { Name = "Kayak", Description = "A boat", Category = "WaterSports", Price = 55 }, new Product { Name = "Ola", Description = "Adeus", Category = "Categs", Price = 69 }, new Product { Name = "Adeus", Description = "A booty", Category = "WaterSports", Price = 455 }, new Product { Name = "Olas", Description = "sdfsdf", Category = "Categs", Price = 649 }, new Product { Name = "Olas1", Description = "sdfsdfssss", Category = "Categs1", Price = 649 }, new Product { Name = "Olas2", Description = "sdfsdfddd", Category = "Categs2", Price = 6419 }, new Product { Name = "Olas3", Description = "sdfsdfee", Category = "Categs3", Price = 6492 }, new Product { Name = "Olas4", Description = "sdfsdfaaa", Category = "Categs4", Price = 6493 }, new Product { Name = "Olas5", Description = "sdfsdfdfs", Category = "Categs5", Price = 6494 }, new Product { Name = "Olas6", Description = "sdfsdfssdd", Category = "Categs6", Price = 6495 }, new Product { Name = "Olas7", Description = "sdfsdfaadd", Category = "Categs7", Price = 6496 } ); db.SaveChanges(); }
public static void EnsurePopulated(IApplicationBuilder app) { SportsStoreDbContext context = app .ApplicationServices .CreateScope() .ServiceProvider .GetRequiredService <SportsStoreDbContext>(); if (context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } if (context.Products.Any()) { return; } context.Products.AddRange( new Product { Name = "Kayak", Description = "A boat for one person", Category = "Watersports", Price = 275 }, new Product { Name = "Lifejacket", Description = "Protective and fashionable", Category = "Watersports", Price = 48.95m }, new Product { Name = "Soccer Ball", Description = "FIFA-approved size and weight", Category = "Soccer", Price = 19.50m }, new Product { Name = "Corner Flags", Description = "Give your playing field a professional touch", Category = "Soccer", Price = 34.95m }, new Product { Name = "Stadium", Description = "Flat-packed 35,000-seat stadium", Category = "Soccer", Price = 79500 }, new Product { Name = "Thinking Cap", Description = "Improve brain efficiency by 75%", Category = "Chess", Price = 16 }, new Product { Name = "Unsteady Chair", Description = "Secretly give your opponent a disadvantage", Category = "Chess", Price = 29.95m }, new Product { Name = "Human Chess Board", Description = "A fun game for the family", Category = "Chess", Price = 75 }, new Product { Name = "Bling-Bling King", Description = "Gold-plated, diamond-studded King", Category = "Chess", Price = 1200 } ); context.SaveChanges(); }