无法创建类型的对象' appContext'。使用冗长,但是

发布于 2025-01-30 05:15:51 字数 1357 浏览 5 评论 0原文

我试图进行第一次迁移,并遇到了这个错误。

我使用了:

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();

我没有得到什么问题。请有人帮我。

I tried to do my first migration and I got this error.

I used:

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

The error:

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'.

My 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>();
            });
}

My Startup

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

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

I don't get what is the problem. Please someone help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2025-02-06 05:15:51

尝试指定这两个选项- 项目- startup-project

-p | -project&lt; project&gt;

这是该项目dbContext坐在。

-s | - startup-project&lt; project&gt;

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


您的迁移添加命令可能看起来如下:

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

Try to specify both options --project and --startup-project

-p|--project <PROJECT>

This is the project that DbContext is sitting in.

-s|--startup-project <PROJECT>

This is the project with Depenedency Injection setup, usually the Web project.


Your migrations add command might look like below:

dotnet ef migrations add initial --startup-project AppProject.Web --project AppProject.Core --verbose  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文