How does LINQ behave in systems with multiple DbContext lifetimes?
Updated May 16, 2026
Short answer
LINQ queries are bound to DbContext lifetime; crossing contexts invalidates tracking and caching assumptions.
Deep explanation
Each DbContext maintains its own change tracker and query pipeline. LINQ queries executed under different contexts are independent. Sharing entities or IQueryable across contexts leads to detached entities, stale tracking, or invalid state assumptions. This is common in layered architectures where repositories create separate contexts.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro