DbContextOptionsBuilder 选项Builder optionsBuilder.isConfigured 始终为 false

DbContextOptionsBuilder optionsBuilder optionsBuilder.isConfigured is always false

提问人:cbas007 提问时间:7/26/2023 最后编辑:cbas007 更新时间:7/26/2023 访问量:34

问:

我正在使用两个数据库上下文,一个用于 Identity,一个用于 ef core。ApplicationDbContext intity 框架有效。SKGDMD_WebSiteContext由 EF core 电动工具创建。它说要做我试图安全的事情。但是当我运行它时,我收到一个错误

AggregateException:发生一个或多个错误。(尚未为此 DbContext 配置任何数据库提供程序。可以通过重写“DbContext.OnConfiguring”方法或在应用程序服务提供程序上使用“AddDbContext”来配置提供程序。如果使用“AddDbContext”,则还要确保 DbContext 类型在其构造函数中接受 DbContextOptions<TContext> 对象,并将其传递给 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 文件中拥有所有连接字符串,并在可能的情况下通过程序 .cs 注入它们。

dbcontext ef-core-6.0 C#-10.0

评论

0赞 Ivan Stoev 7/26/2023
看起来您有一个使用此构造函数的代码,而不是从 DI 配置的构造函数。public SKGDMD_WebSiteContext()
0赞 cbas007 7/27/2023
@IvanStoev 我该如何解决这个问题?我是 DI 世界的新手。SKGDMD_WebSiteContext() 用于我的所有存储过程。

答: 暂无答案