DbContextOptionsBuilder optionsBuilder optionsBuilder.isConfigured 始终为 false

问题描述 投票:0回答:0

我使用两个数据库上下文,一个用于 Identity,一个用于 ef core。 ApplicationDbContext 实体框架有效。 SKGDMD_WebSiteContext 由 EF 核心强大工具创建。它说要做我试图确保安全的事情。但是当我运行它时,我收到错误

AggregateException:发生一个或多个错误。 (尚未为此 DbContext 配置数据库提供程序。可以通过重写“DbContext.OnConfiguring”方法或在应用程序服务提供程序上使用“AddDbContext”来配置提供程序。如果使用“AddDbContext”,还请确保您的 DbContext类型在其构造函数中接受 DbContextOptions 对象,并将其传递给 DbContext 的基本构造函数。)

CS 程序

\`var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext\<ApplicationDbContext\>(options =\>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDbContext\<SKGDMD_WebSiteContext\>(options =\>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity\<ApplicationUser\>(options =\> options.SignIn.RequireConfirmedAccount = true)
.AddRoles\<IdentityRole\>()
.AddEntityFrameworkStores\<ApplicationDbContext\>();
builder.Services.AddRazorPages();'

我遇到问题的上下文

public partial class SKGDMD_WebSiteContext : DbContext
{
public SKGDMD_WebSiteContext()
{
}

    public SKGDMD_WebSiteContext(DbContextOptions<SKGDMD_WebSiteContext> options)
        : base(options)
    {
        
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
       
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        OnModelCreatingGeneratedProcedures(modelBuilder);
        OnModelCreatingPartial(modelBuilder);
    }
    
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

}

在程序 CS 中,我还尝试使用 DbContext 更改 SKGDMD_WebSiteContext,这导致了相同的错误。我究竟做错了什么?如果我执行 OnConfiguring 覆盖,它就会起作用。它告诉我不要将其用于生产代码,因此我想按照预期的方式进行设置。 SQL 配置有效,因为它在我登录并执行身份相关任务时有效。我希望将所有连接字符串都放在 json 文件中,并尽可能通过 program.cs 注入它们。

dbcontext ef-core-6.0 c#-10.0
© www.soinside.com 2019 - 2024. All rights reserved.