public void SaveOrder(Order order)
 {
     _db.AttachRange(order.Lines.Select(l => l.Product));
     if (order.OrderID == 0)
     {
         _db.Orders.Add(order);
     }
     _db.SaveChanges();
 }
        public void SaveProduct(Product product)
        {
            if (product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                Product dbEntry = context.Products.FirstOrDefault(p => p
                                                                  .ProductID == product.ProductID);

                if (dbEntry != null)
                {
                    dbEntry.Name        = product.Name;
                    dbEntry.Description = product.Description;
                    dbEntry.Price       = product.Price;
                    dbEntry.Category    = product.Category;
                }
            }

            context.SaveChanges();
        }
Пример #3
0
        public static void EnsureAdd(IApplicationBuilder app)
        {
            SportDbContext context = app.ApplicationServices.GetRequiredService <SportDbContext>();

            context.Database.Migrate();

            if (!context.Products.Any())
            {
                context.Products.AddRange(
                    new Product
                {
                    Name        = "Kayak",
                    Description = "A boat for one persion",
                    Category    = "Watersports",
                    Price       = 440
                },
                    new Product
                {
                    Name        = "Lifejacket",
                    Description = "Protective and Fashioable",
                    Category    = "Watersports",
                    Price       = 12.5M
                },
                    new Product
                {
                    Name        = "Socer Ball",
                    Description = "Fifa-approved size and weight",
                    Category    = "Ball",
                    Price       = 20.75M
                }

                    );

                context.SaveChanges();
            }
        }