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.
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
- 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
Other ways to find it
🧠 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026