Repository navigation
Keep local view bindings when the change tracker is cleared - #39385
Merged
AndriySvyryd merged 2 commits intoOct 23, 2026
Merged
Conversation
- 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
Contributor
There was a problem hiding this comment.
🟡 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
- 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
AndriySvyryd
approved these changes
Oct 23, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #36543
ChangeTracker.Clear()callsStateManager.Clear(resetting: false), which calledResetState()on every set and through thatLocalView.Reset(). That drops theObservableCollection/BindingListand everyCollectionChanged/PropertyChangedsubscriber, so a WPF or WinForms binding toLocal.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 internalLocalView.OnStateManagerCleared(). That sets the count to 0 and raisesCollectionChanged(Reset)plus theCountnotifications. The sets are copied before any events are raised.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.Clearonly resets the sets whenresettingis true (context pooling, unchanged). The temporary state managers used for identity resolution (EntityFinder,ManyToManyLoader) also callClear(resetting: false), and they used to reset the context's local views as well.LocalViewignores entries tracked by those temporary state managers. They reach it through the sharedILocalViewListener, so aLoad(LoadOptions.ForceIdentityResolution)into a detached entity was adding the loaded entities to the context's local view.Sets are reached through a small
internalinterface,ILocalViewOwner, implemented byInternalDbSet, so there's no public API change.Tests in
DataBindingTestBase:ChangeTracker_Clear_empties_existing_local_view_collections_and_keeps_them_boundchecks 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_viewloads a collection into a detached entity withForceIdentityResolution, then checks that the context's local view and its bound collection are unchanged.Local runs:
EFCore.Tests7004 passed;EFCore.InMemory.FunctionalTests28168 passed, 264 skipped, 0 failed.