Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

openiddict

How can I set default scheme for Openiddict-core tables?

Unfortunately EF Core does't have (not that I know) a method that would accept only scheme and (EntityTypeBuilder.ToTable) requires also table name, beside scheme.

like image 714
Makla Avatar asked Sep 09 '25 12:09

Makla


1 Answers

There's nothing OpenIddict-specific to handle that, but it's easily achievable using the regular EF hooks. Here's an example:

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;
                }
            }
        }
    }
}
like image 186
Kévin Chalet Avatar answered Sep 12 '25 21:09

Kévin Chalet