如何在Identity RoleClaims表(asp网络核心)中添加新列 [英] How to add new colum into Identity RoleClaims table (asp net core)
问题描述
我试图在身份(asp网络核心)RoleClaims表中添加一列,但是我发现内容只是为了扩展角色和用户类,而不是RoleClaims.有人可以帮忙提供示例或指出内容吗?
I'm trying to add a column to the identity (asp net core) RoleClaims table but I find content just to extend the roles and users classes and not to RoleClaims. Could someone help with examples or point out content.
推荐答案
您将需要创建一个新类来扩展 RoleClaim
.这是一个示例示例,如果您的密钥类型是 string
:
You would need to create a new class to extend the RoleClaim
. Here is an example of how to do it if your key type is string
:
public class ApplicationRoleClaim : IdentityRoleClaim<string>
{
public virtual ApplicationRole Role { get; set; }
}
您可以将任何想要的新属性添加到此类中,然后创建迁移以将它们添加为表列.
You can add whatever new properties you want to this class then create a migration to add them as table columns.
您还需要告诉您的 IdentityDbContext
也要使用此新类.这是文档中的示例:
You would also need to tell your IdentityDbContext
to use this new class as well. Here is an example from the docs:
public class ApplicationDbContext
: IdentityDbContext<
ApplicationUser, ApplicationRole, string,
ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin,
ApplicationRoleClaim, ApplicationUserToken>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
使用自定义的 ApplicationRoleClaim
类,您也可以覆盖 OnModelCreating
.这是来自文档的示例:
With your custom ApplicationRoleClaim
class, you could override OnModelCreating
as well. This is an example from the docs:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
⋮
modelBuilder.Entity<IdentityRoleClaim<string>>(b =>
{
b.ToTable("MyRoleClaims");
});
⋮
}
参考: 查看全文