我在更新/编辑分配了新角色或更改角色(1 或 2)的用户时遇到问题。角色保持不变,没有异常或错误。
注意:我在这里发布屏幕截图是因为我想准确地展示我所看到的。
我有一个User
包含角色和城市相关类的类:
public class User
{
public City CityId { get; set; }
public virtual Role Role { get; set; }
public bool IsInRole(int id)
{
return Role != null && Role.Id == id;
}
}
该Role
表只有 2 个角色,ID=1 的管理员和 ID=2 的访客。
我在更新现有用户的角色时遇到问题。
我在这里尝试做的是,我想通过简单地更新用户并在角色字段中传递 1 来为用户授予管理员权限,如下所示:
我可以通过此字段将 1 或 2 个 ID 传递给控制器,并设法通过角色 ID 获取“管理员”:
因为我在 USER 中有所需的 ROLE 对象:
我仍然无法在数据库中更新具有 Admin 角色的用户。它仍然具有Guest
角色 ID。
同样的场景是CityID
在User
课堂上。
任何帮助,将不胜感激
问题是您必须先找到 id,然后确保 Role 表不应被修改:
public void Update(User users // this is your object`enter code here)
{
using(_db)
{
User user= db.User.Find(users.id);
user.Role_id = users.Role_id; // this is what you want to change.
_db.Entry(Role).State = EntityState.Unchanged;
_db.SaveChanges();
}
}
希望这会有所帮助。干杯
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句