public int CreateRole(MyRoles role, int[] function) { using (var scope = EFContextFactory.GetCurrentDbContext().Database.BeginTransaction()) { try { foreach (var item in function) { DataRepository.Add <RoleFunction>(new RoleFunction() { RoleID = role.Id, FunctionID = item, }); } DataRepository.Add <MyRoles>(role); scope.Commit(); LogHelper.WriteLog("添加角色及权限", "1" + role.ObjectToJson()); return(1); } catch (Exception ex) { ErrorLogHelper.WriteLog(ex); scope.Rollback(); return(0); } } }
public int EditRole(MyRoles role, int[] function) { using (var scope = EFContextFactory.GetCurrentDbContext().Database.BeginTransaction()) { try { DataRepository.DB.Database.ExecuteSqlCommand("delete from rolefunction where roleid='" + role.Id + "'"); foreach (var item in function) { DataRepository.DB.Set <RoleFunction>().Add(new RoleFunction { RoleID = role.Id, FunctionID = item, }); } List <string> fields = new List <string>(); fields.Add("Description"); fields.Add("Name"); DataRepository.UpdateSetEntityFields <MyRoles>(role, fields); DataRepository.DB.SaveChanges(); scope.Commit(); LogHelper.WriteLog("修改角色及权限", "1", function + role.ObjectToJson()); return(1); } catch (Exception ex) { ErrorLogHelper.WriteLog(ex); scope.Rollback(); return(0); } } }