public List <TblDyeingOrdersMainDetail> SaveDyeingOrder(TblDyeingOrdersHeader header, List <TblDyeingOrdersMainDetail> Maindetails) { using (var entities = new WorkFlowManagerDBEntities()) { if (header.DyeingProductionOrder != 0) { var h = entities.TblDyeingOrdersHeaders.SingleOrDefault(x => x.DyeingProductionOrder == header.DyeingProductionOrder); if (h != null) { h.DocPlan = header.DocPlan; h.TransactionDate = header.TransactionDate; h.Vendor = header.Vendor; } } else { entities.AddToTblDyeingOrdersHeaders(header); } entities.SaveChanges(); foreach (var item in Maindetails) { if (item.DyeingProductionOrder == 0) { item.DyeingProductionOrder = header.DyeingProductionOrder; entities.AddToTblDyeingOrdersMainDetails(item); } else { var mainDetailRow = (from d in entities.TblDyeingOrdersMainDetails where d.TransId == item.TransId && d.TransactionType == item.TransactionType && d.DyeingProductionOrder == item.DyeingProductionOrder select d).SingleOrDefault(); if (mainDetailRow != null) { GenericUpdate(mainDetailRow, item, entities); } else { entities.AddToTblDyeingOrdersMainDetails(item); } } entities.SaveChanges(); if (item.DyeingProductionOrder != 0) { foreach (var items in item.TblDyeingOrdersDetails.ToList()) { var dyeingOrderDetails = (from d in entities.TblDyeingOrdersDetails where d.Iserial == items.Iserial && d.TransactionType == item.TransactionType select d).SingleOrDefault(); if (dyeingOrderDetails != null) { GenericUpdate(dyeingOrderDetails, items, entities); } else { items.TblDyeingOrdersMainDetail = null; entities.AddToTblDyeingOrdersDetails(items); } entities.SaveChanges(); } } } } return(Maindetails); }