所以,我试图在我的应用程序上实现不同类型的用户,首先,让我们说只有一种用户:
public class ApplicationUser : IdentityUser
{
// Other Properties
public int TeacherID { get; set; }
[ForeignKey("TeacherID ")]
public virtual Teacher Teacher { get; set; }
}
public class Teacher
{
[Key]
public int TeacherID { get; set; }
public int UserID { get; set; }
// Other properties
[ForeignKey("UserID")]
public virtual ApplicationUser User { get; set; }
}
这两个实体之间存在一对一的关系,但是如果有多种类型的用户呢?我不能在用户实体上拥有ForeignKey,我想我的方向是错误的。
我虽然为此使用角色,所以每个角色都有管理员,教师,学生和不同类型的角色,但如果我想为每种角色存储额外的属性会发生什么?
public class IdentityUserRole<TKey>
{
public IdentityUserRole();
// Resumen:
// RoleId for the role
public virtual TKey RoleId { get; set; }
//
// Resumen:
// UserId for the user that is in the role
public virtual TKey UserId { get; set; }
}
我的意思是,我可以扩展IdentityUserRole类并添加更多属性,但是如何为每种角色添加属性?
答案 0 :(得分:4)
为此目的使用角色当然是有意义的,但它确实意味着您可以分配多个角色。所以用户可以是教师和学生,但这可能发生。
如果要为角色类添加额外的属性,请按照与用户相同的方式完成。像这样创建自己的Role
版本:
public class ApplicationRole : IdentityRole
{
public string bool CanJuggle { get; set; }
}
你需要一个RoleManager类来使用它:
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole> store)
: base(store)
{ }
//snip
}
不要忘记你的背景需要改变:
public class YourContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
//snip
}
认为涵盖了所有相关部分。