Unable to create object of type ‘ApplicationDbContext’ – Updating to .NET Core 3.0

Hi everyone,

I’ve just updated to .net core 3.0 and have been hitting a few issues. I hit this one while trying to add a migration:

Unable to create an object of type ‘ApplicationDbContext’. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Adding -verbose to this allowed me to narrow the cause down a little further:

PM> add-migration add-propertyid-to-entities-for-events -verbose
Using project ‘PropertyApi’.
Using startup project ‘PropertyApi’.
Build started…
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec –depsfile C:\Users\Celeste\Documents\repos\property-api\PropertyApi\bin\Debug\netcoreapp3.0\PropertyApi.deps.json –additionalprobingpath C:\Users\Celeste\.nuget\packages –additionalprobingpath “C:\Program Files\dotnet\sdk\NuGetFallbackFolder” –runtimeconfig C:\Users\Celeste\Documents\repos\property-api\PropertyApi\bin\Debug\netcoreapp3.0\PropertyApi.runtimeconfig.json C:\Users\Celeste\.nuget\packages\microsoft.entityframeworkcore.tools\3.0.0\tools\netcoreapp2.0\any\ef.dll migrations add add-propertyid-to-entities-for-events –json –verbose –no-color –prefix-output –assembly C:\Users\Celeste\Documents\repos\property-api\PropertyApi\bin\Debug\netcoreapp3.0\PropertyApi.dll –startup-assembly C:\Users\Celeste\Documents\repos\property-api\PropertyApi\bin\Debug\netcoreapp3.0\PropertyApi.dll –project-dir C:\Users\Celeste\Documents\repos\property-api\PropertyApi\ –language C# –working-dir C:\Users\Celeste\Documents\repos\property-api –root-namespace PropertyApi
Using assembly ‘PropertyApi’.
Using startup assembly ‘PropertyApi’.
Using application base ‘C:\Users\Celeste\Documents\repos\property-api\PropertyApi\bin\Debug\netcoreapp3.0’.
Using working directory ‘C:\Users\Celeste\Documents\repos\property-api\PropertyApi’.
Using root namespace ‘PropertyApi’.
Using project directory ‘C:\Users\Celeste\Documents\repos\property-api\PropertyApi\’.
Finding DbContext classes…
Finding IDesignTimeDbContextFactory implementations…
Finding application service provider…
Finding Microsoft.Extensions.Hosting service provider…
Using environment ‘Development’.
System.ArgumentNullException: Value cannot be null. (Parameter ‘implementationInstance’)

The key part here are the fact that it can’t find the config values and that it’s attempting to use the ‘Development’ env whereas it should be looking for ‘Local’.

It seems that during the upgrade all of my environment variables have been wiped. All that was required was to run the following (change local to match your environment):

$Env:ASPNETCORE_ENVIRONMENT = “Local”

Running the add-migration command should now user the local configuration. Thanks to the following link for the solution:
https://stackoverflow.com/a/50507486/522859


Posted

in

, ,

by

Comments

One response to “Unable to create object of type ‘ApplicationDbContext’ – Updating to .NET Core 3.0”

  1. James Hicks Avatar
    James Hicks

    All I had to do was add a parameterless constructor e.g. public MyDbContext() { } – I did find this using the -verbose parameter – thanks for that info

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: