[英]Is it possible to add Roles as foreign keys in the ApplicationUser class?
I want to show all the users with their roles. 我想向所有用户展示他们的角色。 I am thinking of doing something like this in the ApplicationUser class:
我正在考虑在ApplicationUser类中执行以下操作:
public string RoleId {get; set;}
[ForeignKey("RoleId")]
public UserManager Role {get; set;}
Of course this doesn't return the correct results, but I think there might be a workaround... 当然,这不会返回正确的结果,但是我认为可能有解决方法...
The default identity schema uses a UserRoles
table to associate a user's roles to them. 默认身份架构使用
UserRoles
表将用户角色与其相关联。 If you implemented a custom IdentityDbContext<> you could create the foreign key relationship between your ApplicationUser
, UserRole
and IdentityRole
implementations. 如果实现了自定义IdentityDbContext <> ,则可以在
ApplicationUser
, UserRole
和IdentityRole
实现之间创建外键关系。
public class ApplicationUser
{
public List<UserRole> UserRoles { get; set; }
}
public class UserRole
{
public string UserId { get; set; }
[ForeignKey(nameof(UserId))]
public ApplicationUser User { get; set }
public string RoleId { get; set; }
[ForeignKey(nameof(RoleId))]
public IdentityRole Role { get; set }
}
IQueryable<ApplicationUser> GetUsers(IdentityDb db)
{
return db.Users.Include(u => u.UserRoles).ThenInclude(ur => ur.Role);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.