/// <summary> /// 进行用户与角色关联 /// </summary> /// <param name="data"></param> /// <returns></returns> public JsonResult UpdateUserRoles2(UserRoleList data) { try { if (data == null) { throw new Exception("提供的更新列表不存在。"); } UserDbContext udb = new UserDbContext("ConnStr1"); List <UserRole> list = udb.GetUserRolesByUserID(data.UserID); //先删除 foreach (UserRole d in list) { udb.DeleteUserRole(d); } //再增加 foreach (string str in data.RoleIDList) { UserRole ur = new UserRole(); ur.UserID = data.UserID; ur.RoleID = str; udb.AddUserRole(ur); } udb.SaveChanges(); //udb.DeleteUserRole() return(Json(data)); } catch (Exception ex) { if (ex.Message != null) { } Exception ex1 = MisBase.BaseUT.GetRootException(ex); throw; } }