如何在 EF Core 中为 Openiddict-core 表设置默认方案

How to set default scheme for Openiddict-core tables in EF Core

如何为 Openiddict-core table 设置默认方案?

不幸的是,EF Core 没有(我不知道)只接受方案的方法,并且(EntityTypeBuilder.ToTable)除了方案之外还需要 table 名称。

没有特定于 OpenIddict 的东西来处理这个问题,但使用常规的 EF 挂钩可以轻松实现。这是一个例子:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions options)
        : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        foreach (var entity in builder.Model.GetEntityTypes())
        {
            for (var type = entity; type != null; type = type.BaseType)
            {
                if (type.ClrType.Assembly == typeof(OpenIddictApplication).Assembly)
                {
                    entity.SqlServer().Schema = "security";

                    break;
                }
            }
        }
    }
}