Search code examples
entity-framework-coredbcontextidentityserver4

IdentityServer4 with a customise ConfigurationDBContext


Is it Okay if I customise the ConfigurationDBContext? I have already created one, the code is below

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

I noticed that ConfigurationDBContext requires special DbContextOptions

DbContextOptions<ConfigurationDbContext>

I am bit confused now. Before i go any further just want to check has anybody done this before? Or if anyone can point out to project or tutorial regarding this.


Solution

  • The definition of IConfigurationDbContext from IdentityServer4.EntityFramework

    public interface IConfigurationDbContext : IDisposable
    {
        DbSet<Client> Clients { get; set; }
        DbSet<IdentityResource> IdentityResources { get; set; }
        DbSet<ApiResource> ApiResources { get; set; }
    
        int SaveChanges();
        Task<int> SaveChangesAsync();
    }
    

    This interface is there to allow developers to customize the context. If you donot want to inherit from ConfigurationDbContext(bind to Ef.Core.DbContext), you can build your own implementation of IConfigurationDbContext just need to add IDbContextFactory<MyConfigurationDbContext>

    public class IdentityConfigurationDbContextFactory : IDbContextFactory<MyConfigurationDbContext>
    {
        public MyConfigurationDbContext Create(DbContextFactoryOptions options)
        {
            //....
        }
    }