public TblDyeingPlanHeader SaveDyeingHeaderList(TblDyeingPlanHeader header, List <TblDyeingPlan> mainDetails, List <TblDyeingSummary> summaryList) { using (var entities = new WorkFlowManagerDBEntities()) { #region PlanWithoutSummary if (header.Iserial != 0) { var h = entities.TblDyeingPlanHeaders.SingleOrDefault(x => x.Iserial == header.Iserial); if (h != null) { h.DocNo = header.DocNo; h.Location = header.Location; h.Brand = header.Brand; h.TblLkpSeason = header.TblLkpSeason; h.TransactionDate = header.TransactionDate; h.TblDyeingType = header.TblDyeingType; } } else { entities.AddToTblDyeingPlanHeaders(header); } foreach (var dyeingPlanRow in mainDetails) { if (dyeingPlanRow.Iserial == 0) { dyeingPlanRow.DyeingHeader = header.Iserial; entities.AddToTblDyeingPlans(dyeingPlanRow); } else { var mainDetailRow = (from d in entities.TblDyeingPlans where d.DyeingHeader == header.Iserial && d.Iserial == dyeingPlanRow.Iserial select d).SingleOrDefault(); if (mainDetailRow != null) { // MainDetailRow.TransactionDate = item.TransactionDate; } else { entities.AddToTblDyeingPlans(dyeingPlanRow); } } entities.SaveChanges(); if (dyeingPlanRow.DyeingHeader != 0) { foreach (var dyeingDetailsPlanRow in dyeingPlanRow.TblDyeingPlanDetails.ToList()) { var dyeingOrderDetails = (from d in entities.TblDyeingPlanDetails 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; } else { dyeingDetailsPlanRow.TblDyeingPlan = null; entities.AddToTblDyeingPlanDetails(dyeingDetailsPlanRow); } entities.SaveChanges(); } } } #endregion PlanWithoutSummary #region SummarySaving foreach (var summaryRow in summaryList) { if (summaryRow.Iserial != 0) { var querySummaryrow = (from d in entities.TblDyeingSummaries where d.Iserial == summaryRow.Iserial select d).SingleOrDefault(); if (querySummaryrow != null) { querySummaryrow.BatchNo = summaryRow.BatchNo; querySummaryrow.CalculatedTotalQty = summaryRow.CalculatedTotalQty; querySummaryrow.Color = summaryRow.Color; querySummaryrow.DyeingClass = summaryRow.DyeingClass; querySummaryrow.DyedFabric = summaryRow.DyedFabric; querySummaryrow.Unit = summaryRow.Unit; querySummaryrow.FabricCode = summaryRow.FabricCode; querySummaryrow.Vendor = summaryRow.Vendor; } } else { summaryRow.TblDyeingPlanHeader = null; summaryRow.DyeingHeader = header.Iserial; entities.AddToTblDyeingSummaries(summaryRow); } entities.SaveChanges(); foreach (var planLotsMasterRow in summaryRow.TblDyeingPlanLotsMasters) { if (planLotsMasterRow.Iserial != 0) { var queryPlanLotsMasterRow = (from d in entities.TblDyeingPlanLotsMasters where d.Iserial == planLotsMasterRow.Iserial select d).SingleOrDefault(); if (queryPlanLotsMasterRow != null) { queryPlanLotsMasterRow.FabricCode = planLotsMasterRow.FabricCode; queryPlanLotsMasterRow.FromColor = planLotsMasterRow.FromColor; queryPlanLotsMasterRow.FabricLot = planLotsMasterRow.FabricLot; queryPlanLotsMasterRow.AvaliableQuantity = planLotsMasterRow.AvaliableQuantity; queryPlanLotsMasterRow.Config = planLotsMasterRow.Config; queryPlanLotsMasterRow.RequiredQuantity = planLotsMasterRow.RequiredQuantity; queryPlanLotsMasterRow.Unit = planLotsMasterRow.Unit; } } else { planLotsMasterRow.TblDyeingSummary = null; planLotsMasterRow.DyeingsSummaryPlanIserial = summaryRow.Iserial; entities.AddToTblDyeingPlanLotsMasters(planLotsMasterRow); } entities.SaveChanges(); foreach (var planLotDetailsRow in planLotsMasterRow.TblDyeingPlanLotsDetails) { if (planLotDetailsRow.Iserial != 0) { var queryPlanLotsDetailsRow = (from d in entities.TblDyeingPlanLotsDetails where d.Iserial == planLotDetailsRow.Iserial select d).SingleOrDefault(); if (queryPlanLotsDetailsRow != null) { queryPlanLotsDetailsRow.DeliveryDate = planLotDetailsRow.DeliveryDate; queryPlanLotsDetailsRow.RequiredQuantity = planLotDetailsRow.RequiredQuantity; queryPlanLotsDetailsRow.SalesOrder = planLotDetailsRow.SalesOrder; queryPlanLotsDetailsRow.Saved = planLotDetailsRow.Saved; } } else { planLotDetailsRow.TblDyeingPlanLotsMaster = null; planLotDetailsRow.FabricLotMasterIserial = planLotsMasterRow.Iserial; entities.AddToTblDyeingPlanLotsDetails(planLotDetailsRow); } } } } entities.SaveChanges(); #endregion SummarySaving return(header); } }