public bool UpdateMenu(MenuViewModel menuViewModel, out string reason) { reason = string.Empty; var menu = _demandDbContext.Menus.FirstOrDefault(item => item.Id == menuViewModel.Id); if (menu == null) { reason = "未查找到该功能菜单"; return(false); } //仅名称变了 才需要判断重复 if (menuViewModel.Name != menu.Name) { var sameNameMenu = _demandDbContext.Menus.FirstOrDefault(item => item.Name == menuViewModel.Name); if (sameNameMenu != null) { reason = "已存在名称:" + sameNameMenu.Name; return(false); } } var newMenu = AutoMapper.Mapper.Map <Menu>(menuViewModel); EntityUpdateHelper.EntityToEntity(newMenu, menu); _demandDbContext.SaveChanges(); return(true); }
public bool UpdateRole(RoleViewModel roleViewModel, out string reason) { reason = string.Empty; if (roleViewModel.Id == 1) { reason = "管理员角色不允许修改"; return(false); } var role = _demandDbContext.Roles.Include(item => item.RoleMenus).FirstOrDefault(item => item.Id == roleViewModel.Id); if (role == null) { reason = "未查找到该角色"; return(false); } //仅名称变了 才需要判断重复 if (roleViewModel.Name != role.Name) { var sameNameRole = _demandDbContext.Roles.FirstOrDefault(item => item.Name == roleViewModel.Name); if (sameNameRole != null) { reason = "已存在名称:" + roleViewModel.Name; return(false); } } //foreach (var rolemenu in role.RoleMenus) //{ // _demandDbContext.RoleMenus.Remove(rolemenu); //} role.RoleMenus.Clear(); _demandDbContext.SaveChanges(); var newRole = AutoMapper.Mapper.Map <Role>(roleViewModel); EntityUpdateHelper.EntityToEntity(newRole, role); role.CreateTime = DateTime.Now; _demandDbContext.SaveChanges(); return(true); }