efcore: Unable to create migrations after upgrading from EF Core 2.0 to 2.1
Describe what is not working as expected. dotnet ef migrations list command throws an error after upgrading from dotnet core 2.0 to 2.1 and ef core 2.1
If you are seeing an exception, include the full exceptions details (message and stack trace).
An error occurred while accessing the IWebHost on class ‘Program’. Continuing wi thout the application service provider. Error: Value cannot be null. Parameter name: configuration Unable to create an object of type ‘xxx’. Add an implementation of ‘IDesignTimeDbContextFactory<xxx>’ to the project, or see https://g o.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at desig n time.
Exception message:
An error occurred while accessing the IWebHost on class 'Program'. Continuing wi
thout the application service provider. Error: Value cannot be null.
Parameter name: configuration
Unable to create an object of type 'xxx'. Add an implementation of
'IDesignTimeDbContextFactory<xxx>' to the project, or see https://g
o.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at desig
n time.
Stack trace:
Steps to reproduce
Include a complete code listing (or project/solution) that we can run to reproduce the issue.
Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.
BuildWebHost(args).Run() was replaced with
CreateWebHostBuilder(args)
.Build()
.Run();
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
Further technical details
EF Core version: 2.1 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 7 IDE: Visual Studio 2017 15.7.3
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 20 (1 by maintainers)
@Tarig0 With 2.1, either CreateWebHostBuilder or BuildWebHost can be used–see https://github.com/aspnet/EntityFrameworkCore/pull/10100
@narind3rs The problem is that when EF calls either CreateWebHostBuilder or BuildWebHost it does so without running Main. (This is intentional because EF needs to build the model and use the DbContext without starting the application.) This means that when EF invokes on of these methods the static IConfiguration property is still null–since it is only set in Main. So, you’ll need to either make sure that IConfiguration is set/handled when EF calls one of these methods, or use IDesignTimeDbContextFactory.
As a note for the next guy who may stumble here.
$env:ASPNETCORE_ENVIRONMENT=‘Development’ try setting your env variable before running the add-migration
No, that doesn’t help. I get the exact same error.
I am able to get past the issue by introducing the code below:
This wasn’t required in ef core 2.0. I still get the warning below:
but at least the migrations are working.
Can someone please post the final solution. I have a rough idea, however, I am not able to figure out where to initialize the IConfiguration property exactly.
@Tarig0 @ajcvickers Thank You Both!
Ah that’s where the null error is coming from, yea @narind3rs you should be doing the use function inside your startup class, if at all possible