提问人:user3953989 提问时间:11/17/2023 更新时间:11/17/2023 访问量:18
使用 Postgres 的 EF Core 迁移 尚未为此 DbContext 错误配置数据库提供程序
EF Core Migrations with Postgres No database provider has been configured for this DbContext error
问:
我的解决方案中有两个 .Net 6 项目和 . 有 EntityFramework 和 EF Design Nuget 包,当我在文件夹中运行时出现错误App.Domain
App.Api
App.Domain
dotnet ef migrations add InitialCreate
App.Domain
No database provider has been configured for this DbContext
我很确定这是因为我在 Startup.cs 文件中定义了数据库提供程序,但从 App.Domain 文件夹运行迁移命令。所有 Google 结果都表明我将所有这些移动到我类中的方法中,但这会删除我的 DI 并破坏单元测试。App.Api
OnConfiguring
DbContext
有没有办法解决这个问题,或者是一种或另一种方式?
App.Api 启动 .cs
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<DataContext>(options =>
{
options.UseNpgsql(connectionString);
});
DBContext
public class DataContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>().ToTable("account");
}
}
答: 暂无答案
评论
IDesignTimeDbContextFactory