예제 #1
0
 public static bool SaveProducts(IEnumerable <Products> itemsToSave, Products itemToDelete)
 {
     try
     {
         using (var db = new TradeContext())
         {
             if (itemsToSave != null)
             {
                 foreach (var t in itemsToSave)
                 {
                     db.Products.AddOrUpdate(t);
                 }
             }
             if (itemToDelete != null)
             {
                 db.Entry(itemToDelete).State = EntityState.Deleted;
                 db.SaveChanges();
             }
             //db.Customers.Remove(itemToDelete);
             db.SaveChanges();
         }
         return(true);
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
         return(false);
     }
 }
예제 #2
0
 public static int GetProductCount(Customers customer)
 {
     using (var db = new TradeContext())
     {
         var item = db.Customers.Find(customer.Id);
         var x    = db.Entry(item)
                    .Collection(b => b.Products)
                    .Query()
                    .Count();
         var c = db.Entry(item)
                 .Collection(b => b.Products)
                 .Query()
                 .Sum(z => z.Count);
         return(x + c);
     }
     return(0);
 }