public bool Update(EF.Division entity) { var code = entity.Code.ToUpper(); var item = _dbContext.Divisions.FirstOrDefault(x => x.ID == entity.ID); var kpiLevels = _dbContext.KPILevels.Where(f => f.TableID == item.Code).ToList(); kpiLevels.ForEach(a => { a.TableID = entity.Code; }); try { var SOP = _dbContext.Divisions.Find(entity.ID); SOP.Name = entity.Name; SOP.Code = code; SOP.LevelID = entity.LevelID; _dbContext.SaveChanges(); return(true); } catch (Exception ex) { //logging return(false); } }
public int Add(EF.Division entity) { var code = entity.Code.ToUpper(); List <EF.KPILevel> kpiLevelList = new List <EF.KPILevel>(); if (_dbContext.Divisions.FirstOrDefault(x => x.Code == code) != null) { return(2); } if (_dbContext.KPILevels.FirstOrDefault(x => x.KPICode == entity.Code) != null) { return(2); } //get all kpi var kpis = new KPIAdminDAO().GetAll(); foreach (var kpi in kpis) { var kpilevel = new EF.KPILevel(); kpilevel.TableID = code; kpilevel.KPICode = kpi.Code; kpilevel.Name = kpi.Name; kpilevel.Checked = false; kpiLevelList.Add(kpilevel); } try { entity.Code = entity.Code.ToUpper(); _dbContext.Divisions.Add(entity); _dbContext.KPILevels.AddRange(kpiLevelList); _dbContext.SaveChanges(); return(1); } catch { return(0); } }