About change-tracking
A development skill covering EF Core’s change-tracking internals, including entity states, snapshots, change detection, and identity maps. It supports work on state transitions and graph replacement behavior, with testing guidance for unit and functional tests.
Key features
- 01Tracked entity and state management
- 02Snapshot and notification change detection
- 03Identity maps and shared-identity replacement
- 04Compiled property accessors
- 05Unit and graph-update testing guidance
Use cases
- 01Debugging unexpected entity state transitions
- 02Testing snapshot-based change detection
- 03Validating replacement graphs with shared identities
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ dotnet/ efcore --skill change-trackingAfter installing, type/change-trackingin Claude Code to confirm it loaded.
Details
From the project READMEChange Tracking
Manages entity states and detects changes for SaveChanges().
Core Components
StateManager— owns tracked entries, identity/reference maps, fixup, cascades, notifications, and changed countsInternalEntryBaseand derived classes — own per-entry state, flags, values, snapshots, and ordered state transitionsChangeDetector— compares current values with baselines and reports changes through entry mutation APIsSnapshotFactoryFactorysubclasses — create passive original- and relationship-value baselinesIdentityMap— permits one active entry per key; shared identity pairs a replacement with the priorDeletedentryPropertyAccessorsFactory,ClrPropertyGetterFactoryandClrPropertySetterFactorycompile property accessors for efficient snapshotting and change detection- Ordinals in
indicesparameter specify element at each complex collection depth
- Ordinals in
Change Detection
- Snapshot and notification strategies both call
SetPropertyModified()to keep property flags and entity state consistent. SetEntityState()validates values, updates flags and complex entries, changes state, then runs manager bookkeeping hooks.
Shared-Identity Replacement
SharedIdentityEntry is the invariant that distinguishes an old deleted entry from its replacement with the same identity. When attaching a replacement graph, a deleted nested entry should be reconsidered only when its source entry has a replacement; do not broadly process every deleted node. Likewise, cascading from the old replaced principal must not delete dependents that belong to the replacement graph. Keep synchronous and asynchronous graph-attach paths equivalent and preserve unrelated values in replacement tests so they isolate the identity transition.
Testing
Unit tests: test/EFCore.Tests/ChangeTracking/. Functional tests: test/EFCore.Specification.Tests/GraphUpdates/.
Common Pitfalls
| Pitfall | Solution |
|---|---|
| State-based checks treat every Added/Deleted entry as a replacement | Scope replacement behavior through SharedIdentityEntry and the graph source |
Validation
DetectChanges()identifies modified properties via snapshot comparison- Setting original values marks properties as modified or unchanged based on comparison with current values