我使用的是.net 6,当我试图进行迁移时,遇到了一个问题:
未为此DbContext配置数据库提供程序。可以通过重写“DbContext.OnConfiguring”方法或在应用程序服务提供程序上使用“AddDbContext”来配置提供程序。如果使用“AddDbContext”,那么还要确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。
我在Stack中阅读了另一个6个问题,但是尽管错误消息与解决方案相同,或者问题似乎是相同的,但我认为问题与.net 6预览7有关,请帮助我找到解决方案。
我有三个项目
Jupyter.Core.Api ->有DbConfiguration Jupyter.Core.Data ->有DbContext Jupyter.Core ->有1型号
和
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 In Program.cs中,我的代码如下所示,.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,在包控制台中尝试使用using命令。
添加-迁移“用户”-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类应该有这样的构造器。
public StationContext(DbContextOptions<StationContext> options) : base(options) { }那么,我错过了什么?有什么暗示吗?
发布于 2021-08-22 00:17:55
嗯,我试着找了几个小时的解决方案,在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" />解决了我的问题。
https://stackoverflow.com/questions/68877432
复制相似问题