c# - 在 .net 6 preview 7 中没有为此 DbContext 配置数据库提供程序
问题描述
我正在使用 .net 6,当我尝试进行迁移时,我遇到了一个问题:
没有为此 DbContext 配置数据库提供程序。可以通过覆盖“DbContext.OnConfiguring”方法或在应用程序服务提供程序上使用“AddDbContext”来配置提供程序。如果使用了“AddDbContext”,那么还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。
我在 Stack 中阅读了另外 6 个问题,但虽然错误消息相同但解决方案不同或问题似乎相同,但我认为问题与 .net 6 preview 7 有关,请帮我找到解决方案。
我有 3 个项目
Jupyter.Core.Api -> 有 DbConfiguration
Jupyter.Core.Data -> 有 DbContext
Jupyter.Core -> 有 1 个模型
和
Jupyter.Core.Api 引用 Jupyter.Core.Data
Jupyter.Core.Data 引用 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" />
在 Program.cs 里面的 Jupyter.Core.Api 我的代码是这样的,.net 6 模板中没有 Startup.cs
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。
消息说,No database provider has been configured for this DbContext, but I did it in 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 类应该有这样的构造函数。
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" />
这解决了我的问题。
推荐阅读
- python - 在网站上请求后如何发送电报消息?
- r - 选择最高值和行均值的更快方法
- python - 多线程 Python 插入 Postgree
- resources - 列表中每个任务的不同资源分配
- java - 如何在套接字编程中将我的课程放在客户端中?
- c# - 如何在 C# 中查找多个变量 DateRanges 之间的间隙
- python - 将 Pyside-2 项目导入我由 Django 构建的网站
- javascript - 脚本文件未在 Safari 浏览器的 javascript 中加载
- java - 创建名为“passwordEncoder”的 bean 时出错:请求的 bean 当前正在创建中
- postgresql - 在 PostgreSQL 中将列的数据类型更改为整数(带有 n/a 的列)