EF Core DbContext Connection / Query issue

Was working on .NET Core application using Entity Framework Core as ORM to database. The application consists of multiple DbContext, public / authorized RESTful API with JWT token authentication.

The request & response flow as below:

Request –> JWT Middleware to grab user object –> Authorization Filter to stop multiple login -> Touch Controller -> Repository -> Execute Query -> Response

Issue and Solution:

An exception occurred while iterating over the results of a query for context type. The connection is closed

Check the through LINQ / Lambda query, make sure that has no second query holds queryable object before current queryable object has execute into a list or released.

$One or more errors occurred. (A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext.

As we know EF Core does not support multiple parallel operations being run on the same DbContext instance

A few troubleshoots can carry out:

  • Reduce frontend API calls by consolidating similar API into a single / fewer endpoints
  • Make sure to await all async calls before continuing to use the context instance.
  • Register DbContext as transient service
  • Using IServiceScopeFactory for each thread


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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s