简体   繁体   English

dotnet核心program.cs的行为有所不同-无法创建类型为'AppDbContext'的对象

[英]dotnet core program.cs behaves differently - Unable to create an object of type 'AppDbContext'

I would like to ask on why the >dotnet ef migrations add InitialMigration 我想问一下为什么> dotnet ef迁移会添加InitialMigration

behaves differently on these 2 program.cs code 在这两个program.cs代码上的行为不同

Code 1: 代码1:

 public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

Code 2 代码2

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

With Code-1, I am able to run > dotnet ef migrations add InitialMigration without an error. 使用Code-1,我可以运行> dotnet ef迁移添加InitialMigration,而不会出现错误。 Using the PackageManager Console> add-migration Initial Migration No Error as well. 使用PackageManager控制台> add-migration初始迁移也没有错误。

But with Code-2, I am having an Error on both Pakckage Manager Console and command prompt. 但是,使用Code-2,我在Pakckage Manager控制台和命令提示符上都出现了错误。 Here is the error: 这是错误:

Unable to create an object of type 'AppDbContext'. 无法创建“ AppDbContext”类型的对象。 Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. 将“ IDesignTimeDbContextFactory”的实现添加到项目中,或在设计时查看https://go.microsoft.com/fwlink/?linkid=851728,以获取其他支持的模式。

As I understand it, both codes basically doing the same thing. 据我了解,这两个代码基本上都在做相同的事情。

The convention changed between ASP.NET Core 2.0 and 2.1. 约定在ASP.NET Core 2.0和2.1之间进行了更改。

In 2.1, the static method is supposed to return the IWebHostBuilder (for tooling reasons). 在2.1中,静态方法应该返回IWebHostBuilder (出于工具方面的原因)。

However, you will need the version of dotnet ef that ships as part of the 2.1.300 SDKs (only a release candidate is available at the time of writing) instead of the one currently shipping as CLI tool reference ( DotNetCliToolReference in the project file). 但是,您将需要作为2.1.300 SDK的一部分提供的dotnet ef版本(在编写本文时仅提供一个候选发布版本),而不是当前作为CLI工具参考DotNetCliToolReference的那个版本(项目文件中的DotNetCliToolReference ) 。

This was an issue for the SPA templates that were updated in between releases which now follow the new convention but are targeting 2.0 - https://github.com/aspnet/templating/issues/442 . 这是SPA模板的一个问题,这些SPA模板在两个发行版之间进行了更新,这些发行版现在遵循新的约定,但目标是2.0- https://github.com/aspnet/templating/issues/442

You can easily change your code to be similar to the Code 1 example you provided or update to the RC version of the tooling that is available at the time of writing. 您可以轻松地将代码更改为与您提供的Code 1示例相似,或将其更新为撰写本文时可用的RC版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM