【问题标题】:.net core 2 entity framework migrations not working.net core 2 实体框架迁移不起作用
【发布时间】:2018-04-06 23:47:18
【问题描述】:

我在 .net core 2.0 中添加数据库迁移时遇到问题。 我收到以下错误:

An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: One or more errors occurred. (Webpack dev middleware failed because of an error while loading 'aspnet-webpack'. Error was: Error: Cannot find module 'aspnet-webpack'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\Dries\AppData\Local\Temp\quzf2u2y.rt0:83:19)
    at __webpack_require__ (C:\Users\Dries\AppData\Local\Temp\quzf2u2y.rt0:20:30)
    at createWebpackDevServer (C:\Users\Dries\AppData\Local\Temp\quzf2u2y.rt0:62:26)
    at C:\Users\Dries\AppData\Local\Temp\00o3h05d.izo:114:19
    at IncomingMessage.<anonymous> (C:\Users\Dries\AppData\Local\Temp\00o3h05d.izo:133:38)
    at emitNone (events.js:86:13)
Current directory is: D:\Projects\Jeroen Fiers\Projecten\Fiers\bin\MCD\Debug\netcoreapp2.0
)
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

我的 Program.cs 调整如下:

public class Program{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

奇怪的是迁移之前(曾经)工作过。

【问题讨论】:

  • 检查你的启动项目和在 npm 控制台的下拉列表中选择的项目。对我来说,只有当两者都指向数据访问库(定义对象类)时它才有效
  • 目前只有一个项目:/

标签: c# .net entity-framework asp.net-core asp.net-core-2.0


【解决方案1】:

天哪,真是个愚蠢的错误(我不太明白)。

我正在使用命令dotnet ef migrations add InitialCreate 就像这个链接sais https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations

这给出了发布的错误,但是当我使用 Add-Migration InitialCreate 时它工作得很好吗?

【讨论】:

    【解决方案2】:

    例如,如果您的解决方案中有 2 个项目:UiProject 和 DataProject

    首先将您的 ui 项目设置为启动。然后在 powershell 中转到您的 DataAccess 项目:

    1: dotnet ef migrations add firstMigrate --startup-project ./UiProject
    2: dotnet ef database update --startup-project ./UiProject
    

    【讨论】:

      【解决方案3】:

      检查 Startup.cs 文件的 Configure() 方法中是否有一些数据库初始化代码(例如数据播种方法):如果有,您可以通过删除该代码从那里移到 Program.cs 文件的 Main() 方法中,方法如下:

      public static void Main(string[] args)
      {
          // REPLACE THIS:
          // BuildWebHost(args).Run();
      
          // WITH THIS:
          var host = BuildWebHost(args);
          using (var scope = host.Services.CreateScope())
          {
              // Retrieve your DbContext isntance here
              var dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
      
              // place your DB seeding code here
              DbSeeder.Seed(dbContext);
          }
          host.Run();
      
          // ref.: https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/
      }
      
      public static IWebHost BuildWebHost(string[] args) =>
          WebHost.CreateDefaultBuilder(args)
              .UseStartup<Startup>()
              .Build();
      

      此类问题是由于在 EF Core 2.x 中它们改变了 BuildWebHost 方法的工作方式:现在它在 中调用 Configure 方法Startup.cs 文件,如果该方法包含一些数据库初始化代码,可能会导致意外问题。

      这里的主要问题是,只要种子代码在第一个 dotnet ef migrations add / dotnet ef database update 命令执行之前运行——当数据库还不存在时,这些问题很可能会引发误导性异常:在那之后,他们发现了一个有效的数据库并在不引发任何异常的情况下完成他们的工作。

      有关更多信息,请查看我在此主题上写的 MS official EF Core 1.x to 2.x migration guidethis blog post

      【讨论】:

        【解决方案4】:

        遇到此类错误时,使用 PowerShell 中的 -Verbose 标志或终端中的 --verbose 来发现代码(或者您正在使用的 Nuget 包)中出现的异常会很有帮助启动并终止 EF Core 的管道。它可能与 DbContext 相关或完全不相关。

        Add-Migration MyMigration -Verbose
        

        dotnet ef migrations add MyMigration --verbose
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-11
          • 2021-09-05
          相关资源
          最近更新 更多