小编Yum*_*lie的帖子

更改 IdentityServer4 实体框架表名称

我正在尝试更改由 PersistedGrantDb 和 ConfigurationDb 为 IdentityServer4 创建的默认表名,并让实体框架生成正确的 SQL。例如; 我希望将数据映射到名为IdentityServer4.EntityFramework.Entities.ApiResource的表中ApiResources,而不是使用使用表的实体mytesttable

根据文档,这应该就像为ToTable我想在DBContext's OnModelCreating方法中重新映射的每个实体添加调用一样简单,以覆盖 TableName = EntityName 的默认行为。问题是这确实创建了一个表,mytesttable但实体框架在运行时创建的 SQL 仍然ApiResources在查询中使用,因此失败。

我采取的步骤是我创建了一个DBContext从 IdentityServer 派生的ConfigurationDbContext,以便能够覆盖OnModelCreating和自定义表名:

public class MyTestDbContext : ConfigurationDbContext
{
    public MyTestDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions) : base(options, storeOptions)
    { }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        Console.WriteLine("OnModelCreating invoking...");

        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityServer4.EntityFramework.Entities.ApiResource>().ToTable("mytesttable");

        base.OnModelCreating(modelBuilder);

        Console.WriteLine("...OnModelCreating invoked");
    }
}
Run Code Online (Sandbox Code Playgroud)

我还实现了一个DesignTimeDbContextFactoryBase<MyTestDBContext>类来MyTestDbContext在设计时通过dotnet ef migrations …

c# postgresql entity-framework entity-framework-core identityserver4

4
推荐指数
1
解决办法
2128
查看次数