Example #1
0
 private static void RemoveItems(string orderId)
 {
     using (var db = new OrderContexts())
     {
         var oldItems = db.OrderItems.Where(item => item.OrderId == orderId);
         db.OrderItems.RemoveRange(oldItems);
         db.SaveChanges();
     }
 }
Example #2
0
 public static void UpdateOrder(Order newOrder)
 {
     RemoveItems(newOrder.Id);
     using (var db = new OrderContexts())
     {
         db.Entry(newOrder).State = EntityState.Modified;
         db.OrderItems.AddRange(newOrder.Items);
         db.SaveChanges();
     }
 }
Example #3
0
 //获取对象,进行更改,保存更改,EF框架会更改对应的数据项
 public static void Add(Goods goods)
 {
     try {
         using (var db = new OrderContexts()) {
             db.GoodItems.Add(goods);
             db.SaveChanges();
         }
     }
     catch (Exception e) {
         throw new ApplicationException($"添加错误!");
     }
 }
Example #4
0
 public static Order AddOrder(Order order)
 {
     try
     {
         using (var db = new OrderContexts())
         {
             db.Orders.Add(order);
             db.SaveChanges();
         }
         return(order);
     }
     catch (Exception e)
     {
         throw new ApplicationException($"添加错误: {e.Message}");
     }
 }
Example #5
0
 public static void Add(Customer customer)
 {
     try
     {
         using (var db = new OrderContexts())
         {
             //获取对象,进行更改,保存更改,EF框架会更改对应的数据项
             db.Customers.Add(customer);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new ApplicationException($"添加错误!");
     }
 }
Example #6
0
 public static void RemoveOrder(string id)
 {
     try
     {
         using (var db = new OrderContexts())
         {
             var order = db.Orders.Include("Items").Where(o => o.Id == id).FirstOrDefault();
             db.Orders.Remove(order);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         //TODO 需要更加错误类型返回不同错误信息
         throw new ApplicationException($"删除订单错误!");
     }
 }