When trying to execute an EF Core migration inside the Package Manager console, I got the following error message:
An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Please contact the administrator. The following database migrations are pending:
20190506091835_InitialCreate
Unable to create an object of type 'SampleContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
When browsing through the code to investigate the problem I noticed the following code snippet:
This code snippet was introduced to prevent executing migrations outside the local development environment. So maybe my environment was not correct?
I double checked the ASPNETCORE_ENVIRONMENT setting but this was set to ‘local’.
Why did this still fail?
The problem was that I was not running the application normally but using the Package Manager console to execute a migration. The Package Manager will use the default enviroment, explaining why I hit this error message.
To fix it I had to explicitly set the environment variable inside the Package Manager console:
$env:aspnetcore_environment="local"