public void UpdateOrder(Order newOrder) { //Order oldOrder = GetOrder(newOrder.OrderID); using (var db = new OScontext()) { var oldOrder = db.Orders .Include(o => o.Customer) .Include(o => o.OrderItems) .Include(o => o.OrderItems.Select(item => item.GoodsItem)) .FirstOrDefault(o => o.OrderID == newOrder.OrderID); if (oldOrder == null) { throw new ApplicationException($"修改错误:订单 {newOrder.OrderID} 不存在!"); } for (int i = 0; i < oldOrder.OrderItems.Count; i++) { db.Goods.Remove(oldOrder.OrderItems[i].GoodsItem); db.OrderItems.Remove(oldOrder.OrderItems[i]); } db.Customers.Remove(oldOrder.Customer); db.Orders.Remove(oldOrder); db.Orders.Add(newOrder); db.SaveChanges(); } //orders.Remove(oldOrder); //orders.Add(newOrder); }
public void Sort(Func <Order, Order, int> func) { using (var db = new OScontext()) { db.Orders.ToList().Sort((o1, o2) => func(o1, o2)); db.SaveChanges(); } //Orders.Sort((o1,o2)=>func(o1,o2)); }
public void Sort() { using (var db = new OScontext()) { db.Orders.OrderBy(o => o.OrderID); db.SaveChanges(); } // orders.Sort(); }
public void AddOrder(Order order) { //if (orders.Contains(order)) // throw new ApplicationException($"添加错误: 订单{order.OrderID} 已经存在了!"); using (var db = new OScontext()) { if (db.Orders.Select(o => o.OrderID).Contains(order.OrderID)) { throw new ApplicationException($"添加错误: 订单{order.OrderID} 已经存在了!"); } db.Orders.Add(order); db.SaveChanges(); //orders = db.Orders // .Include(o => o.Customer) // .Include(o => o.OrderItems) // .Include(o => o.OrderItems.Select(item => item.GoodsItem)) // .ToList(); } //orders.Add(order); }
public void Import(string path) { XmlSerializer xs = new XmlSerializer(typeof(List <Order>)); using (FileStream fs = new FileStream(path, FileMode.Open)) { List <Order> temp = (List <Order>)xs.Deserialize(fs); using (var db = new OScontext()) { temp.ForEach(order => { if (!db.Orders.Select(o => o.OrderID).Contains(order.OrderID)) { db.Orders.Add(order); } }); db.SaveChanges(); } } }
public void RemoveOrder(int orderId) { //orders.Remove(order); using (var db = new OScontext()) { var order = db.Orders .Include(o => o.Customer) .Include(o => o.OrderItems) .Include(o => o.OrderItems.Select(item => item.GoodsItem)) .FirstOrDefault(o => o.OrderID == orderId); if (order != null) { for (int i = 0; i < order.OrderItems.Count; i++) { db.Goods.Remove(order.OrderItems[i].GoodsItem); db.OrderItems.Remove(order.OrderItems[i]); } db.Customers.Remove(order.Customer); db.Orders.Remove(order); db.SaveChanges(); } } }