自定义 Asp.Net Core ApiAuthorizationDbContext table 名称

Customise Asp.Net Core ApiAuthorizationDbContext table names

我正在使用 Asp.Net 核心标识,我想更改由默认 ApiAuthorizationDbContext 生成的 tables 名称。为了争论,假设我想将 AspNetRoleClaims 更改为 MyRoleClaims.

我的 DbContext 看起来像这样:

public class MyDbContext : ApiAuthorizationDbContext<ApplicationUser>
{
    public MyDbContext(
        DbContextOptions<MyDbContext> options,
        IOptions<OperationalStoreOptions> operationalStoreOptions)
        : base(options, operationalStoreOptions)
    {

    }

    public DbSet<MyData> MyData { get; set; }
}

我知道,如果我直接从 IdentityDbContext 继承,那么我可以指定某些实体,但我看不到使用 ApiAuthorizationDbContext 来做到这一点的方法。看起来两者之间的唯一区别是以下 tables:

public DbSet<PersistedGrant> PersistedGrants { get; set; }
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }

所以,我想我可以基于 IdentityDbContext 创建我的 DbContext,然后手动添加这些,但这感觉就像我在重新发明轮子。有没有办法指定这些 table 名称,或者我真的需要从 IdentityDbContext 手动构建它?

在您的 ApiAuthorizationDbContext 中覆盖 OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); 

        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
    }