I'm having a problem with adding a database migration in .net core 2.0. I'm getting the following error:
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.
My Program.cs is adjusted to the following:
public class Program{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
The strange part is that the migrations have worked before (one time).
Omg such a stupid mistake (and I don't really understand it).
I was using the command dotnet ef migrations add InitialCreate
like this link sais https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
This gives the error as posted but when I use Add-Migration InitialCreate
it works just fine?
Check if you have some DB initialization code (such as a data-seeding method) in the Startup.cs file's Configure()
method: in case you do, you could fix your issue by removing that code from there and move it into the Program.cs file's Main()
method in the following way:
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();
Such kind of issues are due to the fact that in EF Core 2.x they changed the behaviour of how the BuildWebHost method works: now it invokes the Configure
method in the Startup.cs file, which could lead to unexpected problems if that method contains some database initialization code.
The major issue there is that such problems will most likely raise misleading exceptions whenever the seeding code runs before the first dotnet ef migrations add
/ dotnet ef database update
command execution – when the database doesn't yet exist: after that, they found a valid Database and do their job without raising any exception.
For additional info, check the MS official EF Core 1.x to 2.x migration guide and also this blog post that I wrote on this topic.
for Example if you have 2 project in your solution as : UiProject & DataProject
first set your ui project as startup. then in powershell goto the your DataAccess project then:
1: dotnet ef migrations add firstMigrate --startup-project ./UiProject
2: dotnet ef database update --startup-project ./UiProject
When hitting errors like this it can be helpful to use the -Verbose
flag in PowerShell or --verbose
in the terminal to discover an exception in your code (or maybe a Nuget package you are using) that is being thrown during startup and killing EF Core's pipeline. It could be something related to a DbContext or something completely unrelated.
Add-Migration MyMigration -Verbose
or
dotnet ef migrations add MyMigration --verbose
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.