Skip to main content

EF Core – Unable to create an object of type DbContext

This problem has bitten me more than once, and I can never remember how to fix it. So, why not write a blog post about it!

When running EF Core migrations in a solution, you may come across this error:

There are several apparent causes. However, in my case (every time I have seen this), it has been caused by having multiple startup projects selected in Visual Studio.

To fix this, simply open your Solution properties and set the startup project to ‘Current Selection’.

If you are using clean architecture, or some other architecture that has your startup context and your dbContext in different projects, you will need to set your startup project to your runtime, and the ‘Default Project’ in the package manager console to the project containing your dbContext.