public void AddOrder(OrderModel orderModel) { using (ShopDevEntities db = new ShopDevEntities()) { try { Order order = null; //if (orderModel.OrderId > 0) //{ // order = GetCustomerProduct(db, customerProductModel.ProductID); //} if (orderModel.OrderId == 0) { order = new Order(); orderModel.CopyProperties(order); db.Orders.Add(order); } else { order = db.Orders.Where(m => m.OrderId == orderModel.OrderId).FirstOrDefault(); orderModel.CopyProperties(order); } db.SaveChanges(); List <CustomerProduct> lstNewproducts = db.CustomerProducts.Where(m => m.OrderID == 0).ToList(); lstNewproducts.ForEach(m => m.OrderID = order.OrderId); //foreach (var product in lstNewproducts) //{ // var oldproduct = db.Products.Where(m => m.ProductName == product.ProductName && m.Type == product.Type && m.CategoryID == product.CategoryID).FirstOrDefault(); // if (oldproduct != null) // { // oldproduct.Unit -= 1; // } //} db.SaveChanges(); } catch { } } }