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.
Cannot access a disposed context instanceObject name: 'AppDbContext' 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
- Await all work within the request
- Use IServiceScopeFactory/IDbContextFactory in background work
- Materialise data before returning
Detailed fix by platform
C#
- csharp
using var scope = scopeFactory.CreateScope(); var db = scope.ServiceProvider.GetRequiredService<AppDb>(); await db.SaveChangesAsync();
How to diagnose
- Timing — After the request?
- 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:
Similar errors
- 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
- A second operation was started on this context EF Core: A second operation was started on this context instance before a previous operation completed
- An unhandled error has occurred Blazor: An unhandled error has occurred. Reload 🗙
Most viewed in .NET
- FileNotFoundException assembly Could not load file or assembly 'X, Version=…' or one of its dependencies
- Unable to cast object of type System.InvalidCastException: Unable to cast object of type 'X' to type 'Y'
- NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 14 Oct 2026