Add change tracking support for complex collections #35962
+14,712
−9,432
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.
Part of #31237
Overview
The implementation introduces change-tracking support for complex type collections that enables EF Core to detect modifications, additions, and deletions within (nested) complex type collections. This design leverages ordinal-based tracking and runtime-generated property accessors to efficiently manage state changes in complex type collections.
Change tracking entries
This change introduces
InternalComplexEntry
, likeInternalEntityEntry
it directly tracks the original values and changes to the contained properties and non-collection complex properties. But unlike the latter, it doesn't contain a reference to the tracked object as it could be of a value type.To access nested complex entries for a particular complex type collection the current or original ordinal for the corresponding collection needs to be used.
The change-tracking system employs a dual-ordinal approach to maintain element positioning:
This dual-ordinal system enables the change tracker to correlate elements between their original and current states, facilitating accurate detection of positional changes and element migrations within collections.
Value-Type Support for Observable Collections
The architecture is designed with forward compatibility for value-type collections. The ordinal-based tracking mechanism abstracts away the reference semantics typically required for change detection, allowing the same infrastructure to support both reference-type complex objects and value-type collections. This is achieved through the indices array system that provides positional addressing regardless of the underlying element type.
DetectChanges Algorithm
The
DetectChanges
process for complex type collections implements a matching algorithm that relies on snapshots:Property Accessor Architecture
The property accessor system employs runtime code generation to create efficient access patterns for nested complex type collections:
Indices Array System
Each complex type collection maintains an indices array that contains index values for each level of nesting. For a deeply nested structure like
Order.Details[i].LineItems[j].Properties[k]
, the indices array would contain[i, j, k]
, representing the position at each collection level.