public void UpdateRecord(DeviationViewModel deviationViewModel) { try { List <Mst_ChildDeviation> NewRoiDeviationList = new List <Mst_ChildDeviation>(); List <Mst_ChildDeviation> NewDistanceDeviationList = new List <Mst_ChildDeviation>(); List <Mst_ChildDeviation> NewSanctionDeviationList = new List <Mst_ChildDeviation>(); List <Mst_ChildDeviation> NewTenureDeviationList = new List <Mst_ChildDeviation>(); List <Mst_ChildDeviation> NewLtvDeviationList = new List <Mst_ChildDeviation>(); #region update record in roi details of deviation //update the data in roi Details table foreach (var p in deviationViewModel.roiDeviationDetailsList) { var FindRoibject = _context.Mst_ChildDeviation.Where(x => x.Id == p.ID && x.ParentId == 1).FirstOrDefault(); if (FindRoibject == null) { var roitrnnew = new Mst_ChildDeviation { ParentId = 1, ApproveDistanceLimit = 0, MinRange = p.RoiMinRange, MaxRange = p.RoiMaxRange, UserCategoryId = p.RoiUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; _context.Mst_ChildDeviation.Add(roitrnnew); } else { FindRoibject.MinRange = p.RoiMinRange; FindRoibject.MaxRange = p.RoiMaxRange; FindRoibject.UserCategoryId = p.RoiUserNo; FindRoibject.RecordUpdatedBy = deviationViewModel.UpdatedBy; FindRoibject.RecordUpdated = DateTime.Now; } NewRoiDeviationList.Add(FindRoibject); } #endregion #region roi details remove //take the loop of table and check from list if found in list then not remove else remove from table itself var roitrnobjlist = _context.Mst_ChildDeviation.Where(x => x.ParentId == 1).ToList(); if (roitrnobjlist != null) { foreach (Mst_ChildDeviation item in roitrnobjlist) { if (NewRoiDeviationList.Contains(item)) { continue; } else { _context.Mst_ChildDeviation.Remove(item); } } _context.SaveChanges(); } #endregion roi trn remove #region update record in distance details of deviation //update the data in roi Details table foreach (var p in deviationViewModel.distanceDeviationDetailsList) { var FindDistancebject = _context.Mst_ChildDeviation.Where(x => x.Id == p.ID && x.ParentId == 2).FirstOrDefault(); if (FindDistancebject == null) { var distancetrnnew = new Mst_ChildDeviation { ParentId = 2, ApproveDistanceLimit = deviationViewModel.ApproveDistanceLimit, MinRange = p.DistanceMinRange, MaxRange = p.DistanceMaxRange, UserCategoryId = p.DistanceUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; _context.Mst_ChildDeviation.Add(distancetrnnew); } else { FindDistancebject.MinRange = p.DistanceMinRange; FindDistancebject.MaxRange = p.DistanceMaxRange; FindDistancebject.UserCategoryId = p.DistanceUserNo; FindDistancebject.RecordUpdatedBy = deviationViewModel.UpdatedBy; FindDistancebject.RecordUpdated = DateTime.Now; } NewDistanceDeviationList.Add(FindDistancebject); } #endregion #region distance details remove //take the loop of table and check from list if found in list then not remove else remove from table itself var distancetrnobjlist = _context.Mst_ChildDeviation.Where(x => x.ParentId == 2).ToList(); if (distancetrnobjlist != null) { foreach (Mst_ChildDeviation item in distancetrnobjlist) { if (NewDistanceDeviationList.Contains(item)) { continue; } else { _context.Mst_ChildDeviation.Remove(item); } } _context.SaveChanges(); } #endregion distance trn remove #region update ornament user var ornamentuser = _context.Mst_ChildDeviation.Where(x => x.ParentId == 3).FirstOrDefault(); if (ornamentuser != null) { ornamentuser.UserCategoryId = deviationViewModel.OrnamentUserNo; _context.SaveChanges(); } #endregion #region update record in sanction details of deviation //update the data in roi Details table foreach (var p in deviationViewModel.sanctionDeviationList) { var FindSanctionObject = _context.Mst_ChildDeviation.Where(x => x.Id == p.ID && x.ParentId == 4).FirstOrDefault(); if (FindSanctionObject == null) { var sanctiontrnnew = new Mst_ChildDeviation { ParentId = 4, ApproveDistanceLimit = 0, MinRange = p.SanctionMinRange, MaxRange = p.SanctionMaxRange, UserCategoryId = p.SanctionedUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; _context.Mst_ChildDeviation.Add(sanctiontrnnew); } else { FindSanctionObject.MinRange = p.SanctionMinRange; FindSanctionObject.MaxRange = p.SanctionMaxRange; FindSanctionObject.UserCategoryId = p.SanctionedUserNo; FindSanctionObject.RecordUpdatedBy = deviationViewModel.UpdatedBy; FindSanctionObject.RecordUpdated = DateTime.Now; } NewSanctionDeviationList.Add(FindSanctionObject); } #endregion #region sanction details remove //take the loop of table and check from list if found in list then not remove else remove from table itself var sanctiontrnobjlist = _context.Mst_ChildDeviation.Where(x => x.ParentId == 4).ToList(); if (sanctiontrnobjlist != null) { foreach (Mst_ChildDeviation item in sanctiontrnobjlist) { if (NewSanctionDeviationList.Contains(item)) { continue; } else { _context.Mst_ChildDeviation.Remove(item); } } _context.SaveChanges(); } #endregion sanction trn remove #region update record in tenure details of deviation //update the data in tenure Details table foreach (var p in deviationViewModel.tenureDeviationList) { var FindTenureObject = _context.Mst_ChildDeviation.Where(x => x.Id == p.ID && x.ParentId == 5).FirstOrDefault(); if (FindTenureObject == null) { var tenuretrnnew = new Mst_ChildDeviation { ParentId = 5, ApproveDistanceLimit = 0, MinRange = p.TenureMinRange, MaxRange = p.TenureMaxRange, UserCategoryId = p.TenureUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; _context.Mst_ChildDeviation.Add(tenuretrnnew); } else { FindTenureObject.MinRange = p.TenureMinRange; FindTenureObject.MaxRange = p.TenureMaxRange; FindTenureObject.UserCategoryId = p.TenureUserNo; FindTenureObject.RecordUpdatedBy = deviationViewModel.UpdatedBy; FindTenureObject.RecordUpdated = DateTime.Now; } NewTenureDeviationList.Add(FindTenureObject); } #endregion #region tenure details remove //take the loop of table and check from list if found in list then not remove else remove from table itself var tenuretrnobjlist = _context.Mst_ChildDeviation.Where(x => x.ParentId == 5).ToList(); if (tenuretrnobjlist != null) { foreach (Mst_ChildDeviation item in tenuretrnobjlist) { if (NewTenureDeviationList.Contains(item)) { continue; } else { _context.Mst_ChildDeviation.Remove(item); } } _context.SaveChanges(); } #endregion tenure trn remove #region update exposure of loan var exposure = _context.Mst_ChildDeviation.Where(x => x.ParentId == 6).FirstOrDefault(); if (exposure != null) { exposure.ThreasoldLimit = deviationViewModel.ThresholdLimit; _context.SaveChanges(); } #endregion #region update record in LTV details of deviation //update the data in LTV Details table foreach (var p in deviationViewModel.lTVDeviationDetailsList) { var FindLtvObject = _context.Mst_ChildDeviation.Where(x => x.Id == p.ID && x.ParentId == 7).FirstOrDefault(); if (FindLtvObject == null) { var ltvtrnnew = new Mst_ChildDeviation { ParentId = 7, ApproveDistanceLimit = 0, MinRange = p.LTVMinRange, MaxRange = p.LTVMaxRange, UserCategoryId = p.LTVUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; _context.Mst_ChildDeviation.Add(ltvtrnnew); } else { FindLtvObject.MinRange = p.LTVMinRange; FindLtvObject.MaxRange = p.LTVMaxRange; FindLtvObject.UserCategoryId = p.LTVUserNo; FindLtvObject.RecordUpdatedBy = deviationViewModel.UpdatedBy; FindLtvObject.RecordUpdated = DateTime.Now; } NewLtvDeviationList.Add(FindLtvObject); } #endregion #region tenure details remove //take the loop of table and check from list if found in list then not remove else remove from table itself var ltvtrnobjlist = _context.Mst_ChildDeviation.Where(x => x.ParentId == 7).ToList(); if (ltvtrnobjlist != null) { foreach (Mst_ChildDeviation item in ltvtrnobjlist) { if (NewLtvDeviationList.Contains(item)) { continue; } else { _context.Mst_ChildDeviation.Remove(item); } } _context.SaveChanges(); } #endregion ltv trn remove } catch (Exception ex) { throw ex; } }
public void SaveRecord(DeviationViewModel deviationViewModel) { using (var context = new MangalDBNewEntities()) { using (DbContextTransaction dbTran = context.Database.BeginTransaction()) { try { //save ROI foreach (var p in deviationViewModel.roiDeviationDetailsList) { var roitrn = new Mst_ChildDeviation { ParentId = 1, ApproveDistanceLimit = 0, MinRange = p.RoiMinRange, MaxRange = p.RoiMaxRange, UserCategoryId = p.RoiUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; context.Mst_ChildDeviation.Add(roitrn); } //Save Distance foreach (var p in deviationViewModel.distanceDeviationDetailsList) { var distancetrn = new Mst_ChildDeviation { ParentId = 2, ApproveDistanceLimit = deviationViewModel.ApproveDistanceLimit, MinRange = p.DistanceMinRange, MaxRange = p.DistanceMaxRange, UserCategoryId = p.DistanceUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; context.Mst_ChildDeviation.Add(distancetrn); } //Save Distance var ornamenttrn = new Mst_ChildDeviation { ParentId = 3, ApproveDistanceLimit = 0, MinRange = 0, MaxRange = 0, UserCategoryId = deviationViewModel.OrnamentUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; context.Mst_ChildDeviation.Add(ornamenttrn); //Save Sanction loan amount foreach (var p in deviationViewModel.sanctionDeviationList) { var sanctiontrn = new Mst_ChildDeviation { ParentId = 4, ApproveDistanceLimit = 0, MinRange = p.SanctionMinRange, MaxRange = p.SanctionMaxRange, UserCategoryId = p.SanctionedUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; context.Mst_ChildDeviation.Add(sanctiontrn); } //Save Sanction loan amount foreach (var p in deviationViewModel.tenureDeviationList) { var sanctiontrn = new Mst_ChildDeviation { ParentId = 5, ApproveDistanceLimit = 0, MinRange = p.TenureMinRange, MaxRange = p.TenureMaxRange, UserCategoryId = p.TenureUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; context.Mst_ChildDeviation.Add(sanctiontrn); } //save thresold amount var thrsholdntrn = new Mst_ChildDeviation { ParentId = 6, ApproveDistanceLimit = 0, MinRange = 0, MaxRange = 0, UserCategoryId = 0, ThreasoldLimit = deviationViewModel.ThresholdLimit, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; context.Mst_ChildDeviation.Add(thrsholdntrn); //Save LTV % foreach (var p in deviationViewModel.lTVDeviationDetailsList) { var sanctiontrn = new Mst_ChildDeviation { ParentId = 7, ApproveDistanceLimit = 0, MinRange = p.LTVMinRange, MaxRange = p.LTVMaxRange, UserCategoryId = p.LTVUserNo, ThreasoldLimit = 0, RecordCreatedBy = deviationViewModel.CreatedBy, RecordCreated = DateTime.Now, RecordUpdatedBy = deviationViewModel.UpdatedBy, RecordUpdated = DateTime.Now, }; context.Mst_ChildDeviation.Add(sanctiontrn); } context.SaveChanges(); dbTran.Commit(); } catch (DbEntityValidationException ex) { dbTran.Rollback(); throw ex; } } } }