/// <summary> /// 编辑一个模块 /// </summary> /// <param name="validationErrors">返回的错误信息</param> /// <param name="db">数据上下文</param> /// <param name="entity">一个模块</param> /// <returns>是否编辑成功</returns> public bool Edit(ref ValidationErrors validationErrors, SysEntities db, SysMenu entity) { /* * 不操作 原有 现有 * 增加 原没 现有 * 删除 原有 现没 */ if (entity == null) { return(false); } int count = 1; SysMenu editEntity = repository.Edit(db, entity); List <string> addSysRoleId = new List <string>(); List <string> deleteSysRoleId = new List <string>(); DataOfDiffrent.GetDiffrent(entity.SysRoleId.GetIdSort(), entity.SysRoleIdOld.GetIdSort(), ref addSysRoleId, ref deleteSysRoleId); if (addSysRoleId != null && addSysRoleId.Count() > 0) { foreach (var item in addSysRoleId) { SysRole sys = new SysRole { Id = item }; db.SysRole.Attach(sys); editEntity.SysRole.Add(sys); count++; } } if (deleteSysRoleId != null && deleteSysRoleId.Count() > 0) { List <SysRole> listEntity = new List <SysRole>(); foreach (var item in deleteSysRoleId) { SysRole sys = new SysRole { Id = item }; listEntity.Add(sys); db.SysRole.Attach(sys); } foreach (SysRole item in listEntity) { editEntity.SysRole.Remove(item);//查询数据库 count++; } } if (count == repository.Save(db)) { return(true); } else { validationErrors.Add("编辑模块出错了"); } return(false); }
/// <summary> /// 设置一个模块 /// </summary> /// <param name="validationErrors">返回的错误信息</param> /// <param name="entity">一个模块</param> /// <returns>是否设置成功</returns> public bool SetSysMenu(ref ValidationErrors validationErrors, SysRole entity) { bool bResult = false; int count = 0; using (TransactionScope transactionScope = new TransactionScope()) { try { SysRole editEntity = repository.GetById(db, entity.Id); List <string> addSysMenuId = new List <string>(); List <string> deleteSysMenuId = new List <string>(); if (entity.SysMenuId != null) { addSysMenuId = entity.SysMenuId.Split(',').ToList(); } if (entity.SysMenuIdOld != null) { deleteSysMenuId = entity.SysMenuIdOld.Split(',').ToList(); } DataOfDiffrent.GetDiffrent(addSysMenuId, deleteSysMenuId, ref addSysMenuId, ref deleteSysMenuId); if (addSysMenuId != null && addSysMenuId.Count() > 0) { foreach (var item in addSysMenuId) { SysMenu sys = new SysMenu { Id = item }; db.SysMenu.Attach(sys); editEntity.SysMenu.Add(sys); count++; } } if (deleteSysMenuId != null && deleteSysMenuId.Count() > 0) { List <SysMenu> listEntity = new List <SysMenu>(); foreach (var item in deleteSysMenuId) { SysMenu sys = new SysMenu { Id = item }; listEntity.Add(sys); db.SysMenu.Attach(sys); } foreach (SysMenu item in listEntity) { editEntity.SysMenu.Remove(item);//查询数据库 count++; } } if (count > 0 && count == repository.Save(db)) { transactionScope.Complete(); bResult = true; } else if (count == 0) { validationErrors.Add("数据没有改变"); } } catch (Exception ex) { Transaction.Current.Rollback(); ExceptionsHander.WriteExceptions(ex); validationErrors.Add("编辑出错了。原因" + ex.Message); } } return(bResult); }