Scaffolding a Read Action in MVC "No parameterless constructor defined for this object."

I came across this error on an ASP.NET Core MVC application due to the failure to connect to the database. I spent hours checking my models and dbContext class only to realize there was nothing wrong with them. The application was just failing to connect to the database. There were two places I had to check.

  1. Startup.cs - the database context should be registered with the correct connection string. enter image description here

  2. Appsetting.json - the connection string should be correctly typed. enter image description here


The error message was slightly misleading. There was a parameter less constructor required but it was not the model it was the datacontext that needs it.


There is also a scenario with aspnet core where the Program class isn't exposing IWebHostBuilder so that design time dbcontext creation can function properly.

I spent a few hours trying to solve this error with an aspnet core 2.2 mvc app.

Take a look at this link to understand how ef core tooling works. Re-writing my Program class to support application services solved it for me since my dbcontext didn't use a parameterless constructor. https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation