Entity framework core 扩展Identity Server 4的配置和操作数据上下文

Entity framework core 扩展Identity Server 4的配置和操作数据上下文,entity-framework-core,identityserver4,entity-framework-migrations,dbcontext,Entity Framework Core,Identityserver4,Entity Framework Migrations,Dbcontext,我想定制Identity Server 4的配置和操作数据上下文 我只让您查看配置存储的代码,因为代码非常相似 这里是我的定制商店: internal class MyConfigurationDbContext : ConfigurationDbContext { public MyConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOption

我想定制Identity Server 4的配置和操作数据上下文

我只让您查看配置存储的代码,因为代码非常相似

这里是我的定制商店:

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

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

        modelBuilder.HasDefaultSchema("configuration");
    }
}
但在这种情况下,我得到了这个错误:

Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext]' while attempting to activate 'My.IdentityServer.DataLayer.Repository.Contexts.MyConfigurationDbContext'.
我怎样才能解决它


谢谢

正如您正确猜测的那样,您必须在上下文构造函数中使用
DbContextOptions
type for options参数

但为了能够调用基构造函数,而不是默认的非泛型构造函数,您应该使用上下文类型作为泛型类型参数从泛型继承上下文:

内部类MyConfigurationDbContext:ConfigurationDbContext
{
公共MyConfigurationDbContext(DbContextOptions选项、ConfigurationStoreOptions存储选项)
:base(选项、存储选项)
{
}
// ...
}

我是多么愚蠢。。。我没有想到我可以改变我的自定义上下文的继承。。。它起作用了
builder.AddConfigurationStore<MyConfigurationDbContext>(options =>
{
    options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
        sql => sql.MigrationsAssembly(MIGRATION_ASSEMBLY));
});
Add-Migration InitialIdentityServerPersistedGrantDbMigration -Context MyConfigurationDbContext -OutputDir Data/Migrations/IdentityServer/PersistedGrantDb
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext]' while attempting to activate 'My.IdentityServer.DataLayer.Repository.Contexts.MyConfigurationDbContext'.