简体   繁体   English

ASP.NET WebAPI DbContext - 无法创建 object 类型/未配置数据库提供程序

[英]ASP.NET WebAPI DbContext - Unable to create an object of type / No database provider has been configured

I've been following this tutorial: YT Tutorial (starts at my issue) with the exception that I'm on .NET6.我一直在关注本教程: YT 教程(从我的问题开始) ,但我使用的是 .NET6。 When I add a migration The two errors I can get are either:当我添加迁移时我会得到的两个错误是:

  • No database provider has been configured for this DbContext (...)没有为此 DbContext 配置数据库提供程序 (...)
  • Unable to create an object of type 'PmrDbContext'.无法创建“PmrDbContext”类型的 object。 (...) <- this happens if I leave out a default constructor (...) <- 如果我遗漏默认构造函数,就会发生这种情况

Program.cs:程序.cs:

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var app = builder.Build();
string connectionString = builder.Configuration.GetConnectionString("DevConnection");
builder.Services.AddDbContext<PmrDbContext>(options => options.UseSqlServer(connectionString));

PmrDbContext.cs: PmrDbContext.cs:

public class PmrDbContext : DbContext
{
    // default constructor there - one or the other error

    public PmrDbContext(DbContextOptions<PmrDbContext> options) : base(options)
    {

    }

    public DbSet<Employee> Employees { get; set; }
}

ConnectionString:连接字符串:

"ConnectionStrings": {
  "DevConnection": "Server=(local)\\sqlexpress;Database=PMRHomeDB;Trusted_Connection=True;MultipleActiveResultSets=True;"
 }

Do I need to create a DB first?我需要先创建一个数据库吗? Is this not a code-first EF solution?这不是代码优先的 EF解决方案吗?

It is not possible to change Services after Build(), so I moved the AddDbContext before it - also leaving out the default constructor in my DbContext class.无法在 Build() 之后更改服务,因此我将 AddDbContext 移到它之前 - 还省略了我的 DbContext class 中的默认构造函数。

string connectionString = builder.Configuration.GetConnectionString("DevConnection");
builder.Services.AddDbContext<PmrDbContext>(options => options.UseSqlServer(connectionString));
var app = builder.Build();

I ran the project and it was working.我运行了这个项目,它正在运行。 Also the Add-Migration also worked this way. Add-Migration 也以这种方式工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET API:尚未为此DbContext配置任何数据库提供程序 - ASP.NET API: No database provider has been configured for this DbContext 使用实体框架配置ASP.NET Core:“尚未为此DbContext配置数据库提供程序” - Configure ASP.NET Core with Entity Framework: “No database provider has been configured for this DbContext” ASP.NET核心InvalidOperationException:&#39;尚未为此DbContext配置数据库提供程序。 - ASP.NET core InvalidOperationException: 'No database provider has been configured for this DbContext./ 在 ASP.NET Core 2.1 中没有为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext in ASP.NET Core 2.1 尚未为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序 - InvalidOperationException: No database provider has been configured for this DbContext Azure KeyVault for DBContext:“尚未为此DbContext配置数据库提供程序” - Azure KeyVault for DBContext: “No database provider has been configured for this DbContext” C#:从 Net Core 中的 DbContext 继承,构造函数:没有为此 DbContext 配置数据库提供程序 - C#: Inherit from DbContext in Net Core, Constructor: No database provider has been configured for this DbContext 无法运行命令。 错误:没有为此 DbContext 配置数据库提供程序 - Unable to run commands. Error: No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序。 可以通过覆盖DbContext来配置提供程序 - InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM