可用于identityserver4中ConfigurationDbContext的扩展?

可用于identityserver4中ConfigurationDbContext的扩展?,identityserver4,Identityserver4,ConfigurationDbContext是否有任何扩展,我们可以在使用IdentityServer4时自定义任何其他字段或索引的上下文 是否可以扩展IdentityServer4提供的模型?我想您是在询问如何定制EntityFramework.DbContexts.Config 您可以从下面的代码中获得一些想法 public class CustomConfigurationContextFactory : IDbContextFactory<CustomConfigurationDb

ConfigurationDbContext是否有任何扩展,我们可以在使用IdentityServer4时自定义任何其他字段或索引的上下文


是否可以扩展IdentityServer4提供的模型?

我想您是在询问如何定制EntityFramework.DbContexts.Config

您可以从下面的代码中获得一些想法

public class CustomConfigurationContextFactory : IDbContextFactory<CustomConfigurationDbContext>
{
    /// <summary>
    /// A factory to create an instance of the StudentsContext 
    /// </summary>
    /// <param name="connectionString"></param>
    /// <returns></returns>
    public static CustomConfigurationDbContext Create(string connectionString)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ConfigurationDbContext>();

        optionsBuilder.UseSqlServer(connectionString);

        var context = new CustomConfigurationDbContext(
            optionsBuilder.Options, 
            new ConfigurationStoreOptions());
        context.Database.EnsureCreated();

        return context;
    }

    public CustomConfigurationDbContext Create(DbContextFactoryOptions options)
    {
        var context = new CustomConfigurationDbContext(
            new DbContextOptions<ConfigurationDbContext>(),
            new ConfigurationStoreOptions() 
          );

        context.Database.EnsureCreated();

        return context;
    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.ConfigureClientContext(_storeOptions);
        builder.Entity<Client>().HasIndex(x => x.ClientName).IsUnique();
    }
}

public class CustomConfigurationContextFactory : IDbContextFactory<CustomConfigurationDbContext>
{
    public CustomConfigurationDbContext Create(DbContextFactoryOptions options)
    {
        var context = new CustomConfigurationDbContext(
            new DbContextOptions<ConfigurationDbContext>(),
            new ConfigurationStoreOptions() 
          );

        context.Database.EnsureCreated();

        return context;
    }
}
公共类CustomConfigurationContextFactory:IDbContextFactory
{
/// 
///创建StudentsContext实例的工厂
/// 
/// 
/// 
公共静态CustomConfigurationDbContext创建(字符串连接字符串)
{
var optionsBuilder=new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(connectionString);
var context=新CustomConfigurationDbContext(
选项生成器。选项,
新的ConfigurationStoreOptions());
context.Database.recreated();
返回上下文;
}
公共CustomConfigurationDbContext创建(DbContextFactoryOptions选项)
{
var context=新CustomConfigurationDbContext(
新的DbContextOptions(),
新的ConfigurationStoreOptions()
);
context.Database.recreated();
返回上下文;
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
ConfigureClientContext(\u storeOptions);
builder.Entity().HasIndex(x=>x.ClientName).IsUnique();
}
}
公共类CustomConfigurationContextFactory:IDbContextFactory
{
公共CustomConfigurationDbContext创建(DbContextFactoryOptions选项)
{
var context=新CustomConfigurationDbContext(
新的DbContextOptions(),
新的ConfigurationStoreOptions()
);
context.Database.recreated();
返回上下文;
}
}

OnModelCreating
您可以添加索引,也可以将自定义数据库集添加到自定义配置dbcontext

您可以在Asp.net核心中像这样扩展ConfigurationDbContext类:

    public class MyNewModel
    {
      public int Id { get; set; }
      public string Name { get; set; }
    }

   public class MyOtherNewModel
   {
      public int Id { get; set; }
      public ApiResource ApiResource { get; set; }//<-- This is built-in model for identityserver4.
      public MyNewModel MyNewModel { get; set; }
      public List<ApiClaims> ApiClaims {get; set;} //<-- This will add a "MyOtherNewModelId" field into built-in ApiClaims table.
   }

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

      public DbSet<MyNewModel> MyNewModelTable { get; set; }

      public DbSet<MyOtherNewModel> MyOtherNewModelTable { get; set; }

      //...
    }
您还可以在单独的类库中对其进行扩展,如下所示:
1-创建新的.net核心类库。
2-更改上面的
IdentityProviderDbContext
类,如下所示:

    public class IdentityProviderDbContext : ConfigurationDbContext
    {
    //TODO: This connecton string will be removed as soon as possible.
    string connectionString = "Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true";

    public IdentityProviderDbContext(DbContextOptions<ConfigurationDbContext> options)
        : base(options,
             new ConfigurationStoreOptions()
             {
                 ConfigureDbContext = configureOptions =>
                    configureOptions.UseSqlServer("Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true",
                                                sql => sql.MigrationsAssembly(typeof(IdentityProviderDbContext).GetTypeInfo().Assembly.GetName().Name))
             })
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(connectionString);
    }
    //...
    // The other parts are the same as above code snippet.
4-运行添加迁移命令
完成了

如果要向现有内置模型添加字段,可能应该阻止EF生成
原始类模型
(在
OnConfiguring()
或其他地方),而是引入
派生模型
(从该
原始模型

    public class IdentityProviderDbContext : ConfigurationDbContext
    {
    //TODO: This connecton string will be removed as soon as possible.
    string connectionString = "Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true";

    public IdentityProviderDbContext(DbContextOptions<ConfigurationDbContext> options)
        : base(options,
             new ConfigurationStoreOptions()
             {
                 ConfigureDbContext = configureOptions =>
                    configureOptions.UseSqlServer("Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true",
                                                sql => sql.MigrationsAssembly(typeof(IdentityProviderDbContext).GetTypeInfo().Assembly.GetName().Name))
             })
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(connectionString);
    }
    //...
    // The other parts are the same as above code snippet.
class IdentityProviderDbContextFactory : IDesignTimeDbContextFactory<IdentityProviderDbContext>
{
    public IdentityProviderDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ConfigurationDbContext>();
        optionsBuilder.UseSqlServer("Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true");

        return new IdentityProviderDbContext(optionsBuilder.Options);
    }
}