.net 核心 2 entity framework 迁移不工作
.net core 2 entity framework migrations not working
我在 .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[=11=]o3h05d.izo:114:19
at IncomingMessage.<anonymous> (C:\Users\Dries\AppData\Local\Temp[=11=]o3h05d.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();
}
奇怪的是迁移之前(一次)有效。
天啊这么愚蠢的错误(我真的不明白)。
我是这样使用命令 dotnet ef migrations add InitialCreate
link sais https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
这给出了发布的错误,但是当我使用 Add-Migration InitialCreate
它工作正常吗?
检查 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 方法的行为:现在它调用 [= Startup.cs 文件中的 13=] 方法,如果该方法包含一些数据库初始化代码,可能会导致意外问题。
存在的主要问题是,只要种子代码在第一个 dotnet ef migrations add
/ dotnet ef database update
命令执行之前运行时,此类问题很可能会引发误导性异常 – 当数据库尚不存在时:在那之后,他们找到了一个有效的数据库并在没有引发任何异常的情况下完成他们的工作。
有关其他信息,请查看我就此主题撰写的 MS official EF Core 1.x to 2.x migration guide and also this blog post。
例如,如果您的解决方案中有 2 个项目:UiProject 和 DataProject
首先将您的 ui 项目设置为启动项目。然后在 powershell 中转到您的 DataAccess 项目然后:
1: dotnet ef migrations add firstMigrate --startup-project ./UiProject
2: dotnet ef database update --startup-project ./UiProject
遇到此类错误时,在 PowerShell 中使用 -Verbose
标志或在终端中使用 --verbose
会有助于发现代码中的异常(或者可能是您正在使用的 Nuget 包) 在启动期间抛出并终止 EF Core 的管道。它可能与 DbContext 相关或完全不相关。
Add-Migration MyMigration -Verbose
或
dotnet ef migrations add MyMigration --verbose
我在 .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[=11=]o3h05d.izo:114:19
at IncomingMessage.<anonymous> (C:\Users\Dries\AppData\Local\Temp[=11=]o3h05d.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();
}
奇怪的是迁移之前(一次)有效。
天啊这么愚蠢的错误(我真的不明白)。
我是这样使用命令 dotnet ef migrations add InitialCreate
link sais https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
这给出了发布的错误,但是当我使用 Add-Migration InitialCreate
它工作正常吗?
检查 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 方法的行为:现在它调用 [= Startup.cs 文件中的 13=] 方法,如果该方法包含一些数据库初始化代码,可能会导致意外问题。
存在的主要问题是,只要种子代码在第一个 dotnet ef migrations add
/ dotnet ef database update
命令执行之前运行时,此类问题很可能会引发误导性异常 – 当数据库尚不存在时:在那之后,他们找到了一个有效的数据库并在没有引发任何异常的情况下完成他们的工作。
有关其他信息,请查看我就此主题撰写的 MS official EF Core 1.x to 2.x migration guide and also this blog post。
例如,如果您的解决方案中有 2 个项目:UiProject 和 DataProject
首先将您的 ui 项目设置为启动项目。然后在 powershell 中转到您的 DataAccess 项目然后:
1: dotnet ef migrations add firstMigrate --startup-project ./UiProject
2: dotnet ef database update --startup-project ./UiProject
遇到此类错误时,在 PowerShell 中使用 -Verbose
标志或在终端中使用 --verbose
会有助于发现代码中的异常(或者可能是您正在使用的 Nuget 包) 在启动期间抛出并终止 EF Core 的管道。它可能与 DbContext 相关或完全不相关。
Add-Migration MyMigration -Verbose
或
dotnet ef migrations add MyMigration --verbose