Skip to content

Keep local view bindings when the change tracker is cleared - #39385

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
AsadIqbal-Dev:fix/clear-keeps-local-view-bindings
Oct 23, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
AsadIqbal-Dev:fix/clear-keeps-local-view-bindings

Conversation

@AsadIqbal-Dev

@AsadIqbal-Dev AsadIqbal-Dev commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Fixes #36543

ChangeTracker.Clear() calls StateManager.Clear(resetting: false), which called ResetState() on every set and through that LocalView.Reset(). That drops the ObservableCollection/BindingList and every CollectionChanged/PropertyChanged subscriber, so a WPF or WinForms binding to Local.ToObservableCollection() silently stopped updating after a clear, and kept showing the old entities.

Changes:

  • ChangeTracker.Clear() now tells each set's local view that it's empty, through a new internal LocalView.OnStateManagerCleared(). That sets the count to 0 and raises CollectionChanged(Reset) plus the Count notifications. The sets are copied before any events are raised.
  • On Reset, the view's own handler clears the existing observable collection (guarded so it doesn't try to delete the entities), and the binding list follows it. Subscribers stay attached, so entities tracked afterwards show up in the same collections.
  • StateManager.Clear only resets the sets when resetting is true (context pooling, unchanged). The temporary state managers used for identity resolution (EntityFinder, ManyToManyLoader) also call Clear(resetting: false), and they used to reset the context's local views as well.
  • LocalView ignores entries tracked by those temporary state managers. They reach it through the shared ILocalViewListener, so a Load(LoadOptions.ForceIdentityResolution) into a detached entity was adding the loaded entities to the context's local view.

Sets are reached through a small internal interface, ILocalViewOwner, implemented by InternalDbSet, so there's no public API change.

Tests in DataBindingTestBase:

  • ChangeTracker_Clear_empties_existing_local_view_collections_and_keeps_them_bound checks the Reset notification, the emptied observable collection and binding list, that the same instances are returned afterwards, and that a newly added entity appears in them.
  • Loading_with_forced_identity_resolution_does_not_clear_local_view loads a collection into a detached entity with ForceIdentityResolution, then checks that the context's local view and its bound collection are unchanged.

Local runs: EFCore.Tests 7004 passed; EFCore.InMemory.FunctionalTests 28168 passed, 264 skipped, 0 failed.

- ChangeTracker.Clear() reset the local views, dropping their ObservableCollection, BindingList and subscribers, so data bound collections stopped updating
- The views now raise a Reset notification and keep their collections; context pooling still resets them

Fixes dotnet#36543

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical issues remain around temporary state-manager cleanup, mutation during set iteration, and the public API baseline.

3 open issues
What changed in this PR

This pull request preserves existing LocalView-backed observable collections and binding lists when ChangeTracker.Clear() removes tracked entities, while retaining subscribers and supporting subsequent tracking.

Changes:

  • Adds internal state-manager clear notifications for local views.
  • Synchronizes existing bound collections through reset notifications.
  • Preserves context-pooling reset behavior.
  • Adds regression coverage for collection identity, notifications, and post-clear tracking.
File Description
test/​EFCore.Specification.Tests/​DataBindingTestBase.cs Adds regression coverage for retained local-view bindings after clearing.
src/​EFCore/​Internal/​InternalDbSet.cs Forwards state-manager clear notifications to local views.
src/​EFCore/​Internal/​ILocalViewOwner.cs Defines the internal local-view notification contract.
src/​EFCore/​ChangeTracking/​LocalView.cs Preserves bound collections and handles reset notifications.
src/​EFCore/​ChangeTracking/​Internal/​StateManager.cs Notifies local views when a non-resetting state manager is cleared.

Review effort: Lite

Comment thread src/EFCore/ChangeTracking/LocalView.cs Resolved
- Temporary state managers used for identity resolution are cleared with Clear(resetting: false) too, so they no longer touch the context's local views
- Local views ignore entries tracked by those temporary state managers
- Copy the sets before raising notifications, and keep OnStateManagerCleared internal

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Review found only a non-blocking internal API documentation nit, and all approval assessments indicate readiness.

No open issues 🎉

3 resolved since last review

Review effort: Lite

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) October 23, 2026 00:24
@AndriySvyryd
AndriySvyryd merged commit 0d69ac7 into dotnet:main Oct 23, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

context.ChangeTracker.Clear() shouldn't reset LocalViews

3 participants