public int Update(WorkflowSettingHeader wfHdr, List <WorkflowSettingModel> lsdetails, out string errMsg) { errMsg = string.Empty; tb_workflowSetting wfdetil = new tb_workflowSetting(); imap = config.CreateMapper(); using (TDMDBEntities context = new TDMDBEntities()) { var _exist = context.tb_workflowSettingHdr.SingleOrDefault(x => x.Id == wfHdr.Id); using (var transaction = context.Database.BeginTransaction()) { try { _exist.TypeID = wfHdr.TypeID; _exist.Version = wfHdr.Version; _exist.ApprovalLevel = wfHdr.ApprovalLevel; _exist.ModifiedBy = "SYSTEM"; _exist.ModifiedDate = DateTime.Now; result_affected += context.SaveChanges(); var toBeDelete = context.tb_workflowSetting.Where(x => x.HeaderID == wfHdr.Id); if (toBeDelete != null) { context.tb_workflowSetting.RemoveRange(toBeDelete); context.SaveChanges(); } foreach (var item in lsdetails) { wfdetil = imap.Map <WorkflowSettingModel, tb_workflowSetting>(item); wfdetil.HeaderID = wfHdr.Id; wfdetil.CreatedBy = "SYSTEM"; wfdetil.CreatedDate = DateTime.Now; context.tb_workflowSetting.Add(wfdetil); result_affected += context.SaveChanges(); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); errMsg = ex.Message; } } } return(result_affected); }
public int Insert(WorkflowSettingHeader wfhdr, List <WorkflowSettingModel> lsmodel, out string errMsg) { errMsg = string.Empty; tb_workflowSetting wfdetil = new tb_workflowSetting(); imap = config.CreateMapper(); int _hdrId = 0; using (TDMDBEntities context = new TDMDBEntities()) { using (var transaction = context.Database.BeginTransaction()) { try { var _cekstillEmpty = context.tb_workflowSettingHdr.Count(); tb_workflowSettingHdr _entHdr = new tb_workflowSettingHdr(); _entHdr.ApprovalLevel = wfhdr.ApprovalLevel; _entHdr.Version = wfhdr.Version; _entHdr.TypeID = wfhdr.TypeID; _entHdr.CreatedBy = wfhdr.CreatedBy; _entHdr.CreatedDate = DateTime.Now; _entHdr.IsActive = true; context.tb_workflowSettingHdr.Add(_entHdr); result_affected += context.SaveChanges(); _hdrId = _entHdr.Id; foreach (var item in lsmodel) { wfdetil = imap.Map <WorkflowSettingModel, tb_workflowSetting>(item); wfdetil.HeaderID = _hdrId; wfdetil.CreatedBy = "SYSTEM"; wfdetil.CreatedDate = DateTime.Now; context.tb_workflowSetting.Add(wfdetil); result_affected += context.SaveChanges(); } if (_cekstillEmpty > 0) { if (NonActivateSetting(_entHdr, _hdrId) > 0) { transaction.Commit(); } else { transaction.Rollback(); } } else { transaction.Commit(); } } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } return(result_affected); }