[英]ASP.NET Core: Error when using a custom DbContext while also using Identity
I've created a dbContext based on an existing Azure SQL Database using Entity Framework.我已经使用实体框架基于现有的 Azure SQL 数据库创建了一个 dbContext。 I added this database to my app's services as follows:
我将此数据库添加到我的应用程序的服务中,如下所示:
public void ConfigureServices(IServiceCollection services)
{
//Identity Database Context
services.AddDbContext<IdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DataDb"),
optionsBuilders =>
optionsBuilders.MigrationsAssembly("WebPortal"))
);
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityDbContext>()
.AddDefaultTokenProviders();
//Custom Database Context
services.AddDbContext<CustomDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CustomDb"))
);
services.AddMvc();
}
When I try to run this I get the following error message:当我尝试运行它时,我收到以下错误消息:
InvalidOperationException: The DbContextOptions passed to the IdentityDbContext constructor must be a DbContextOptions.
InvalidOperationException:传递给 IdentityDbContext 构造函数的 DbContextOptions 必须是 DbContextOptions。 When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions parameter rather than a non-generic DbContextOptions parameter.
注册多个 DbContext 类型时,请确保每个上下文类型的构造函数都有一个 DbContextOptions 参数,而不是一个非通用的 DbContextOptions 参数。
The constructor for my custom Context does have a parameter:我的自定义上下文的构造函数确实有一个参数:
public CustomDbContext(DbContextOptions<CustomDbContext> options)
: base(options)
{
}
Why am I getting the error?为什么我收到错误?
I had the same problem.我有同样的问题。 my scenario was that, i needed two Context ReadDataContext and WriteDataContext ,i solved that exception with bottom Contexts
我的情况是,我需要两个 Context ReadDataContext和WriteDataContext ,我用底部 Contexts 解决了这个异常
public class ReadOnlyDataContext : DbContext
{
public ReadOnlyDataContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.RemovePluralizingTableNameConvention();
Assembly assemblyWithConfigurations = typeof(TaskConfiguration).Assembly;
modelBuilder.ApplyConfigurationsFromAssembly(assemblyWithConfigurations);
}
}
Pay attention to the DataContext Constructor
注意DataContext构造函数
public class WriteDataContext : DbContext, IContext
{
public WriteDataContext(DbContextOptions<WriteDataContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.RemovePluralizingTableNameConvention();
Assembly assemblyWithConfigurations = typeof(TaskConfiguration).Assembly;
modelBuilder.ApplyConfigurationsFromAssembly(assemblyWithConfigurations);
}
}
And for registeration
和注册
services.AddDbContext<DataContext>(opt =>
{
opt.UseSqlServer(configuration.GetConnectionString("CommanderConnection"));
opt.LogTo(Console.WriteLine).EnableSensitiveDataLogging();
});
services.AddDbContext<ReadOnlyDataContext>(opt =>
{
opt.UseSqlServer(configuration.GetConnectionString("CommanderConnection"));
opt.LogTo(Console.WriteLine).EnableSensitiveDataLogging();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.