ChangeTracker retained entity references after DbContext disposal in EF Core 7.0.0
0 reputation · 24 Jan 2020, 02:03 UTC
0 reputation · 24 Jan 2020, 02:03 UTC
EF Core 7.0.0 ChangeTracker behavior is documented as retaining entity entries after DbContext disposal, leading to memory growth with repeated context lifetimes. The issue is reported as fixed in EF Core 7.0.1 by clearing internal state on disposal.
The default for AutoDetectChangesEnabled remains enabled in EF Core 7.0, which simplifies change tracking but can add overhead for large read-only queries. There is no announced plan to change this default in upcoming releases.
For applications that create short-lived DbContext instances with large tracking sets, the interaction between disposal clearing and automatic change detection is relevant.
What is the current design stance on keeping AutoDetectChangesEnabled enabled by default given read-heavy performance considerations? Is the ChangeTracker clearing implemented in 7.0.1 considered sufficient for all disposal paths? Are there documented guidelines for opting out of automatic detection without affecting update scenarios?
The EF Core team keeps AutoDetectChangesEnabled enabled by default because it guarantees that any changes to tracked entities are automatically persisted during SaveChanges(). For read‑heavy workloads the recommended way to avoid the overhead is to use AsNoTracking() on queries, not to flip the global default.
DbContext.Dispose(), causing memory growth.using blocks, manual Dispose() calls, and DI‑scoped lifetimes that end with disposal.AutoDetectChangesEnabled stops the automatic snapshot comparison, but you must call ChangeTracker.DetectChanges() manually before SaveChanges() to ensure updates are saved.dotnet list package or check your .csproj)..AsNoTracking() to the LINQ query to bypass the ChangeTracker entirely.AutoDetectChangesEnabled globally or per context, remember to invoke context.ChangeTracker.DetectChanges()SaveChanges().using or relying on DI scoped lifetime) – the 7.0.1 fix ensures no retained references after disposal.If you still observe memory growth after upgrading to 7.0.1 and applying AsNoTracking(), please confirm how the DbContext lifetime is managed (e.g., transient, scoped, singleton) and whether any entities are held in static fields or long‑lived services, as external references can prevent GC reclamation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.