gpt4 book ai didi

identityserver4 - Identityserver4 中的 ConfigurationDbContext 可以使用扩展吗?

转载 作者:行者123 更新时间:2023-12-04 16:09:44 25 4
gpt4 key购买 nike

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

OnModelCreating 您可以添加索引,也可以将自定义 Dbset 添加到自定义配置 dbcontext

关于identityserver4 - Identityserver4 中的 ConfigurationDbContext 可以使用扩展吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44727309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com