示例#1
0
 public List <Product> GetProducts()
 {
     using (var shopEntities = new ShopEntities())
     {
         return(shopEntities.Products.ToList());
     }
 }
示例#2
0
        public void UpdateProduct(string name, decimal price, string currency, int stock)
        {
            using (var shopEntities = new ShopEntities())
            {
                var existingProduct = shopEntities.Products.FirstOrDefault(x => x.Name == name);

                if (existingProduct != null)
                {
                    existingProduct.Stock += stock;
                }
                else
                {
                    shopEntities.Products.Add(new Product()
                    {
                        Name     = name,
                        Price    = price,
                        Currency = currency,
                        Stock    = stock
                    });
                }

                shopEntities.SaveChanges();
            }
        }