[Solved] InvalidOperationException: The DbContext of type cannot be pooled because it does not have a public constructor accepting a single parameter of type DbContextOptions or has more than one constructor

This issue I encountered when I tried to scaffold database tables to Entity framework core entities in a Asp.Net Core project using scaffold-dbcontext command. After scaffolding, when I tried to run my project I could see this run time error.

Error Details:

InvalidOperationException: The DbContext of type ‘IQContext’ cannot be pooled because it does not have a public constructor accepting a single parameter of type DbContextOptions or has more than one constructor.
Microsoft.EntityFrameworkCore.Internal.DbContextPool..ctor(DbContextOptions options)

InvalidOperationException: The DbContext of type ‘Context’ cannot be pooled because it does not have a public constructor accepting a single parameter of type DbContextOptions or has more than one constructor.
Microsoft.EntityFrameworkCore.Internal.DbContextPool..ctor(DbContextOptions options)

Problem

If we observe the issue, we can understand the issue is with context class. Also it indicates that, the issue is with constructor. Mainly issue is concerned with more than one constructor.

Here is the screenshot of DbContext class, where the issue is throwing. If we observe the screen shot we can able to see there are two constructors. These class generated by Scaffolding command to generate context class from database first approach.

Solution

To solve this issue, I supposed remove empty constructor with no parameter. Once I removed the non-parametrized constructor, application could run without any run time errors.

Summary

In this issue, If we could read the error description, we will get a hint to solve the issue. Most of the errors would be self explanatory and few error descriptions would be off course confusing.

Default image
Sreenivasa Rangan TR
Software professional with 7+ years of experience in all phases of software development with robust problem-solving skills and proven experience in creating and designing software in a test-driven environment.

Leave a Reply