You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core 8控制台应用生成EF Core迁移时DbContext创建失败的问题求助

.NET Core 8控制台应用生成EF Core迁移时DbContext创建失败的问题求助

我创建了一个.NET Core 8控制台应用,在尝试生成EF Core迁移的时候碰到了一个错误,具体提示如下:

Error: Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[HealthCheck.Console.Context.SystemHealthContext]' while attempting to activate 'HealthCheck.Console.Context.SystemHealthContext'.' was thrown while attempting to create an instance. 关于设计时支持的不同模式,可参考官方相关文档。

下面是我的DbContext类代码:

public class SystemHealthContext : DbContext
{
    public SystemHealthContext(DbContextOptions<SystemHealthContext> options) : base(options) { }

    public DbSet<ServerMasterInfo> ServerMasterInfo { get; set; }
}

这是我在Program类里注册DbContext的代码:

var configuration = new ConfigurationBuilder()
    .SetBasePath(AppContext.BaseDirectory)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .Build();

var services = new ServiceCollection();

var connectionString = configuration.GetConnectionString("DbConnection");


services.AddDbContext<SystemHealthContext>(options =>
    options.UseSqlServer(connectionString));

services.AddTransient<SystemHealthCheck>();
var serviceProvider = services.BuildServiceProvider();
var systemHealthCheck = serviceProvider.GetRequiredService<SystemHealthCheck>();

迁移错误的详细截图:
Migration Error Details

提前感谢各位大佬的帮忙!

备注:内容来源于stack exchange,提问作者Mithun Chandra Howlader

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 09:18:05