无法创建 'AppContext' 类型的对象。使用详细但

Unable to create an object of type 'AppContext'. Using verbose but

我尝试进行第一次迁移,但遇到此错误。

我用过:

dotnet ef migrations add initial --project AppProject.Core --verbose  

错误:

Finding application service provider in assembly 'AppProject.Core'...
Finding Microsoft.Extensions.Hosting service provider...
-> No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
-> No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'AppContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'AppContext'.

我的Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

我的创业公司

services.AddDbContext<AppContext>(options =>
{
        options.UseNpgsql(connection);
});

services.AddIdentity<User, IdentityRole<Guid>>()
        .AddEntityFrameworkStores<SysFishingContext>()
        .AddDefaultTokenProviders();

我不明白这是什么问题。请有人帮助我。

尝试同时指定选项 --project--startup-project

-p|--project <PROJECT>

这是DbContext所在的项目。

-s|--startup-project <PROJECT>

这是具有依赖注入设置的项目,通常是 Web 项目。


您的 migrations add 命令可能如下所示:

dotnet ef migrations add initial --startup-project AppProject.Web --project AppProject.Core --verbose