Skip to content

Add change tracking support for complex collections #35962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AndriySvyryd
Copy link
Member

@AndriySvyryd AndriySvyryd commented Apr 16, 2025

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, like InternalEntityEntry 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:

  • Original Ordinals: Preserve the initial ordering of elements as they existed when first tracked by the context. These serve as immutable identifiers that remain constant throughout the entity's lifetime in the change tracker.
  • Current Ordinals: Reflect the present ordering of elements after any modifications. These values update dynamically as elements are added, removed, or reordered within collections.

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:

  1. Snapshot Comparison: The system maintains snapshots of collection states and compares current values against these snapshots to identify modifications.
  2. Element Matching: Deleted and added elements are matched using a combination of:
    • Ordinal position correlation
    • Reference comparison for identifying moved elements for reference types
    • Null-matching heuristics that mark a property as modified if it transitions to or from a null value

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.

@AndriySvyryd AndriySvyryd force-pushed the Issue31237_ChangeTracking branch 2 times, most recently from d56cde0 to 56d99e6 Compare May 8, 2025 01:14
@AndriySvyryd AndriySvyryd force-pushed the Issue31237_ChangeTracking branch from 56d99e6 to 547b2c5 Compare May 16, 2025 19:26
@AndriySvyryd AndriySvyryd force-pushed the Issue31237_ChangeTracking branch from 547b2c5 to c103ff1 Compare May 28, 2025 00:48
@AndriySvyryd AndriySvyryd force-pushed the Issue31237_ChangeTracking branch 4 times, most recently from e099818 to cbc8c95 Compare June 20, 2025 05:00
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 20, 2025 05:01
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 20, 2025 05:01
@AndriySvyryd AndriySvyryd force-pushed the Issue31237_ChangeTracking branch from cbc8c95 to 5a16ed7 Compare June 20, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant