asp.net 具有自定义角色的身份
asp.net identity with custom role
我对身份框架和自定义角色有疑问。我的自定义角色包含一组 activity 权限。
我的自定义角色看起来像
public class MyRole : IdentityRole
{
public List<MyPermission> Permissions { get; set; }
}
public class MyPermission
{
public int Id { get; set; }
public int Value { get; set; }
}
和这样的 RoleManager
public RoleManager<MyRole> MyRoleManager;
我可以创建新角色,但我无法更新角色权限。
我更新喜欢
myRole.Permissions.Clear();
myRole.Permissions.Add(new MyPermission() { ... });
MyRoleManager.Update(myRole);
已添加新权限,但旧权限仍在数据库中。我怎样才能删除那些旧的。
已编辑:
我刚刚注意到更多问题。当我从 RoleManager 使用 FindById 时,没有从数据库加载权限。
谢谢。
假设您使用的是 Code First,您需要将 DbSet 添加到您的上下文中:
DbSet<MyRole> Roles {get;set;}
DbSet<MyPermission> Permissions {get;set;}
您还需要确保您的 collection 被标记为 virtual
ICollection
:
public class MyRole : IdentityRole
{
public virtual ICollection<MyPermission> Permissions { get; set; }
}
我对身份框架和自定义角色有疑问。我的自定义角色包含一组 activity 权限。
我的自定义角色看起来像
public class MyRole : IdentityRole
{
public List<MyPermission> Permissions { get; set; }
}
public class MyPermission
{
public int Id { get; set; }
public int Value { get; set; }
}
和这样的 RoleManager
public RoleManager<MyRole> MyRoleManager;
我可以创建新角色,但我无法更新角色权限。 我更新喜欢
myRole.Permissions.Clear();
myRole.Permissions.Add(new MyPermission() { ... });
MyRoleManager.Update(myRole);
已添加新权限,但旧权限仍在数据库中。我怎样才能删除那些旧的。
已编辑: 我刚刚注意到更多问题。当我从 RoleManager 使用 FindById 时,没有从数据库加载权限。
谢谢。
假设您使用的是 Code First,您需要将 DbSet 添加到您的上下文中:
DbSet<MyRole> Roles {get;set;}
DbSet<MyPermission> Permissions {get;set;}
您还需要确保您的 collection 被标记为 virtual
ICollection
:
public class MyRole : IdentityRole
{
public virtual ICollection<MyPermission> Permissions { get; set; }
}