public TblDyeingPlanHeaderAcc SaveDyeingHeaderListAcc(TblDyeingPlanHeaderAcc header, List <TblDyeingPlanACC> mainDetails, List <TblDyeingSummaryAcc> summaryList) { using (var entities = new WorkFlowManagerDBEntities()) { #region PlanWithoutSummary if (header.Iserial != 0) { var h = entities.TblDyeingPlanHeaderAccs.SingleOrDefault(x => x.Iserial == header.Iserial); h.DocNo = header.DocNo; h.Location = header.Location; h.Brand = header.Brand; h.TblLkpSeason = header.TblLkpSeason; h.TransactionDate = header.TransactionDate; } else { entities.AddToTblDyeingPlanHeaderAccs(header); } foreach (var dyeingPlanRow in mainDetails) { if (dyeingPlanRow.Iserial == 0) { dyeingPlanRow.DyeingHeader = header.Iserial; entities.AddToTblDyeingPlanACCs(dyeingPlanRow); } else { var mainDetailRow = (from d in entities.TblDyeingPlanACCs where d.DyeingHeader == header.Iserial && d.Iserial == dyeingPlanRow.Iserial select d).SingleOrDefault(); if (mainDetailRow != null) { // MainDetailRow.TransactionDate = item.TransactionDate; } else { entities.AddToTblDyeingPlanACCs(dyeingPlanRow); } } entities.SaveChanges(); if (dyeingPlanRow.DyeingHeader != 0) { foreach (var dyeingDetailsPlanRow in dyeingPlanRow.TblDyeingPlanDetailsAccs.ToList()) { var dyeingOrderDetails = (from d in entities.TblDyeingPlanDetailsAccs where d.Iserial == dyeingDetailsPlanRow.Iserial && d.FabricColorName == dyeingDetailsPlanRow.FabricColorName select d).SingleOrDefault(); if (dyeingOrderDetails != null) { dyeingOrderDetails.BatchNo = dyeingDetailsPlanRow.BatchNo; dyeingOrderDetails.FabricColorValue = dyeingDetailsPlanRow.FabricColorValue; dyeingOrderDetails.BatchNoCreated = dyeingDetailsPlanRow.BatchNoCreated; dyeingOrderDetails.OldColor = dyeingDetailsPlanRow.OldColor; } else { dyeingDetailsPlanRow.TblDyeingPlanACC = null; entities.AddToTblDyeingPlanDetailsAccs(dyeingDetailsPlanRow); } entities.SaveChanges(); } } } #endregion PlanWithoutSummary #region SummarySaving foreach (var summaryRow in summaryList) { if (summaryRow.Iserial != 0) { var querySummaryrow = (from d in entities.TblDyeingSummaryAccs where d.Iserial == summaryRow.Iserial select d).SingleOrDefault(); if (querySummaryrow != null) { querySummaryrow.Color = summaryRow.Color; querySummaryrow.DyedFabric = summaryRow.DyedFabric; querySummaryrow.Unit = summaryRow.Unit; querySummaryrow.CalculatedTotalQty = summaryRow.CalculatedTotalQty; querySummaryrow.FabricCode = summaryRow.FabricCode; querySummaryrow.Vendor = summaryRow.Vendor; } } else { summaryRow.TblDyeingPlanHeaderAcc = null; summaryRow.DyeingHeader = header.Iserial; entities.AddToTblDyeingSummaryAccs(summaryRow); } entities.SaveChanges(); foreach (var planLotsMasterRow in summaryRow.TblDyeingPlanLotsMasterAccs) { if (planLotsMasterRow.Iserial != 0) { var queryPlanLotsMasterRow = (from d in entities.TblDyeingPlanLotsMasterAccs where d.Iserial == planLotsMasterRow.Iserial select d).SingleOrDefault(); queryPlanLotsMasterRow.FabricCode = planLotsMasterRow.FabricCode; queryPlanLotsMasterRow.FabricLot = planLotsMasterRow.FabricLot; queryPlanLotsMasterRow.AvaliableQuantity = planLotsMasterRow.AvaliableQuantity; queryPlanLotsMasterRow.Config = planLotsMasterRow.Config; queryPlanLotsMasterRow.RequiredQuantity = planLotsMasterRow.RequiredQuantity; queryPlanLotsMasterRow.Unit = planLotsMasterRow.Unit; } else { planLotsMasterRow.TblDyeingSummaryAcc = null; planLotsMasterRow.DyeingsSummaryPlanIserial = summaryRow.Iserial; entities.AddToTblDyeingPlanLotsMasterAccs(planLotsMasterRow); } entities.SaveChanges(); foreach (var planLotDetailsRow in planLotsMasterRow.TblDyeingPlanLotsDetailsAccs) { if (planLotDetailsRow.Iserial != 0) { var queryPlanLotsDetailsRow = (from d in entities.TblDyeingPlanLotsDetailsAccs where d.Iserial == planLotDetailsRow.Iserial select d).SingleOrDefault(); queryPlanLotsDetailsRow.DeliveryDate = planLotDetailsRow.DeliveryDate; queryPlanLotsDetailsRow.RequiredQuantity = planLotDetailsRow.RequiredQuantity; queryPlanLotsDetailsRow.SalesOrder = planLotDetailsRow.SalesOrder; queryPlanLotsDetailsRow.Saved = planLotDetailsRow.Saved; } else { planLotDetailsRow.TblDyeingPlanLotsMasterAcc = null; planLotDetailsRow.FabricLotMasterIserial = planLotsMasterRow.Iserial; entities.AddToTblDyeingPlanLotsDetailsAccs(planLotDetailsRow); } } } } entities.SaveChanges(); #endregion SummarySaving return(header); } }