在 .net 6 preview 7 中没有为此 DbContext 配置数据库提供程序

No database provider has been configured for this DbContext in .net 6 preview 7

我正在使用 .net 6,当我尝试进行迁移时遇到问题:

No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

我在 Stack 中阅读了另一个 6 问题,但是虽然错误消息是相同的,但解决方案或问题似乎是相同的,我认为问题与 .net 6 preview 7 有关,帮我找一个解决方案。

我有 3 个项目

Jupyter.Core.Api -> Has DbConfiguration

Jupyter.Core.Data -> Has DbContext

Jupyter.Core -> Has 1 Model

Jupyter.Core.Api references Jupyter.Core.Data

Jupyter.Core.Data references Jupyter.Core

在 Jupyter.Core.Api 中,我有以下与问题相关的 PackageReference:

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.9">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql" Version="5.0.7" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />

在 Jupyter.Core.Data 中,我有以下与问题相关的 PackageReference:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.7.21378.4" />

在Jupyter.Core.Api里面Program.cs我的代码是这样的,没有Startup.cs in .net 6 template

using Jupyter.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddDbContext<StationContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("StationConnection")));

builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "Juptyer.Core.Api", Version = "v1" });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Juptyer.Core.Api v1"));
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

而在 Jupyter.Core.Data 中 StationContext.cs 是这样的。

using System;
using Jupyter.Core.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace Jupyter.Core.Data;

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

    public StationContext(DbContextOptions<StationContext> options) : base(options) { }

    protected override void OnConfiguring(DbContextOptionsBuilder opitionsBuilder)
    {
        base.OnConfiguring(opitionsBuilder);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasAnnotation("Relational:Collation", "en_US.utf8");

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

    public DbSet<User> Users { get; set; }
}

我正在使用 Visual Studio,我正在尝试包控制台中的休闲命令。

add-migration 'User' -Context StationContext

我的默认启动项目设置为 Jupyter.Core.Api,包控制台内设置为 Jupyter.Core.Data。

消息说,没有为此 DbContext 配置数据库提供程序,但我在 Program.cs

中配置了
builder.Services.AddDbContext<StationContext>(options => 
    options.UseNpgsql(builder.Configuration.GetConnectionString("StationConnection")));

它还讨论了“如果使用 'AddDbContext',则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。”我也这么做了。

protected override void OnConfiguring(DbContextOptionsBuilder opitionsBuilder)
{
    base.OnConfiguring(opitionsBuilder);
}

我也知道 DbContext class 应该有这样的构造器。

public StationContext(DbContextOptions<StationContext> options) : base(options) { }

那么,我缺少什么?有什么提示吗?

好吧,我花了几个小时试图找到解决方案,在我将问题发布到此处并尝试了 40 分钟后,我明白了为什么它不起作用。

代码中的所有内容都是正确的,但是 PackageReference 不正确。

在Jupyter.Core.Api而不是这个

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.9">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql" Version="5.0.7" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />

应该是这样的

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.7.21378.4">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-preview7" />

并在 Jupyter.Core.Data 而不是这个

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.7.21378.4" />

应该是这样的

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.7.21378.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-preview7" />

这解决了我的问题。