EF Core InvalidOperationException: A second operation was started on this context instance

`DbContext` is not thread-safe and was shared across concurrent `Task.WhenAll` tasks.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: EF Core InvalidOperationException Platform: DotNet
Common Root Causes:
  • `DbContext` is not thread-safe and was shared across concurrent `Task.WhenAll` tasks.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Await queries sequentially or create per-task contexts via `IDbContextFactory<T>`.
Safe Diagnostic Command:
Check DI lifetime and parallel Task.WhenAll calls
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

`DbContext` is not thread-safe and was shared across concurrent `Task.WhenAll` tasks. Recommended diagnostic: `Check DI lifetime and parallel Task.WhenAll calls`. Remediation: Await queries sequentially or create per-task contexts via `IDbContextFactory<T>`.

Worked Examples

  • Run `Check DI lifetime and parallel Task.WhenAll calls` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-dotnet-efcore-concurrent-dbcontext · Independently verified: 2026-11-25

Related Resources