Search code examples
openiddict

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


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.


Solution

  • 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;
                    }
                }
            }
        }
    }