static public void ModifyOrder(Order newOrder) { using (var context = new OrderServiceContext()) { var order = context.Orders.FirstOrDefault(o => o.Order_ID == newOrder.ID); if (order != null) { order.Name = newOrder.Name; order.Address = newOrder.Address; order.OrderTime = newOrder.OrderTime; var query = context.Items .Where(i => i.OrderID == newOrder.ID); List <DB_OrderItem> items = query.ToList(); foreach (OrderItem item in newOrder.orderItems) { var db_item = new DB_OrderItem() { Name = item.Name, Price = item.Price, Quantity = item.Quantity, OrderID = newOrder.ID }; if (items.Contains(db_item)) { continue; } AddItem(item, newOrder.ID); } } } }
static void AddItem(OrderItem item, int orderID) { var db_item = new DB_OrderItem() { Name = item.Name, Price = item.Price, Quantity = item.Quantity, OrderID = orderID }; using (var context = new OrderServiceContext()) { context.Entry(db_item).State = EntityState.Added; context.SaveChanges(); newItemID = db_item.ItemID; } }