public void DeleteOrder(int orderID) { using (var management = new OrderManagement()) { var order = management.Orders.Include("OrderItems").FirstOrDefault(o => o.ID == orderID); if (order != null) { management.Orders.Remove(order); management.SaveChanges(); } } }
public int AddOrder(Order order) { if (order == null) { throw new ArgumentException("Cannot add an empty order!"); } using (var management = new OrderManagement()) { management.Orders.Add(order); management.SaveChanges(); } return(order.ID); }
public OrderService()//构造函数 { using (var management = new OrderManagement()) { //若无商品(数据库初始化),默认添加商品 if (management.Goodss.Count(goods => true) == 0) { management.Goodss.Add(new Goods() { Name = "milk", Price = 5.3 }); management.Goodss.Add(new Goods() { Name = "cola", Price = 9.2 }); management.SaveChanges(); } } }
public void AlterOrder(int orderID, String alterType, object alterData, int orderItemID = 0) { using (var management = new OrderManagement()) { var order = management.Orders.Include("OrderItems").FirstOrDefault(o => o.ID == orderID); if (order != null) { switch (alterType) { case "customer": order.CustomerID = IsInt(alterData); break; case "quantity": int amount = order.OrderDetails.FirstOrDefault( i => i.ID == orderItemID).Amount;; break; default: throw new ArgumentException("No such operation!"); } management.SaveChanges(); } } }