Cannot access a disposed context instance .NET Origin: Application & API Last updated 14 Oct 2026

ObjectDisposedException: Cannot access a disposed context instance

Code used a DbContext after its scope ended — usually async void, fire-and-forget tasks, or capturing a scoped context in a singleton/background thread.

Exact message you may see Cannot access a disposed context instanceObject name: 'AppDbContext'
Seen on: .NET

Meaning

The request scope disposes the context when the request finishes. Work continuing after that (unawaited tasks, events, background threads) fails.

Common causes

  • async void or un-awaited Task using the context
  • Context captured by a singleton/hosted service
  • Lazy-loading navigation after the request ended

⚡ Quick fix

  1. Await all work within the request
  2. Use IServiceScopeFactory/IDbContextFactory in background work
  3. Materialise data before returning

Detailed fix by platform

C#

  1. csharp
    using var scope = scopeFactory.CreateScope();
    var db = scope.ServiceProvider.GetRequiredService<AppDb>();
    await db.SaveChangesAsync();

How to diagnose

  1. Timing — After the request?
  2. Lifetime — Who holds the context?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

500.19 — HTTP Error 500.19 - Internal Server Error: The requested page cannot be accessed because the related configuration data for the page is invalid (.NET)500.19A second operation was started on this context — EF Core: A second operation was started on this context instance before a previous operation completed (.NET)A second operation wa…An unhandled error has occurred — Blazor: An unhandled error has occurred. Reload 🗙 (.NET)An unhandled error ha…cannot be tracked because another instance with the same key — The instance of entity type 'X' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked (.NET)cannot be tracked bec…DbUpdateException — DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. (.NET)DbUpdateExceptionE0502 — Rust: error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable (Rust)E0502Failed to complete negotiation with the server — SignalR: Error: Failed to complete negotiation with the server / Failed to start the connection (.NET)Failed to complete ne…InvalidOperationException — InvalidOperationException: Unable to resolve service for type 'X' while attempting to activate 'Y' (.NET)InvalidOperationExcep…Cannot ac…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.