asp.net-core - 如何在 Asp.Net Core 2 中正确注册自定义 IdentityServer ConfigurationDbContext?
问题描述
我正在尝试从 IdentityServer 创建自己的 ConfigurationDbContext。
public class IdSrvConfigurationDbContext : ConfigurationDbContext<ConfigurationDbContext>
{
public IdSrvConfigurationDbContext(DbContextOptions<IdSrvConfigurationDbContext> options, ConfigurationStoreOptions storeOptions) : base(options.ChangeOptionsType<ConfigurationDbContext>(), storeOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//mylogic here
base.OnModelCreating(modelBuilder);
}
}
现在在 Startup.cs 我尝试了以下
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddConfigurationStore(options =>
{
// (DbContextOptionsBuilder) paramBuilder
options.ConfigureDbContext = paramBuilder =>
paramBuilder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationAssembly));
});
现在,当我尝试在我的项目上运行迁移时,它会经历所有启动逻辑注入并以以下错误结束:
解决方案
您需要将IdSrvConfigurationDbContext
类型设置为期望 a DbContextOptions<ConfigurationDbContext>
。这是底层ConfigurationDbContext
期望的类型,也是 IdentityServer 将要传递的类型。
通常,您应该始终使用DbContextOptions<T>
与上下文匹配的类型。但是当从现有上下文继承时,这可能有点困难。但在这些情况下,您不必担心太多:键入的选项仅用于区分各种配置的选项。因此,只要您的应用程序中的每个上下文仍然使用单独的类型,就不会有任何问题。
推荐阅读
- html - 尝试为照片添加圆角
- react-native - React Native TextInput 在移动浏览器上不起作用
- ocaml - Ocaml中函数之间的循环依赖
- sql - 是否可以使用多个本机查询来返回 java 对象中的结果集
- json - 如何在表中的 varchar 列中解析此 JSON?雪花
- apache-kafka - 卡夫卡操作
- java - 在 forEach Java 8 中累积
- python - 如何在python中组合列表的相邻相同元素?
- python - 在 TensorFlow 中从 CSV 文件加载大型数据集
- python - Pandas 根据另一列将列值转换为不同的列