Skip to content

Warn and fix identity conflict when owned entity has inconsistent data in DB#38596

Open
AndriySvyryd with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-entity-loading-issue
Open

Warn and fix identity conflict when owned entity has inconsistent data in DB#38596
AndriySvyryd with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-entity-loading-issue

Conversation

Copilot AI commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

When a row has NULL in a non-nullable column of an outer owned entity but non-NULL values in nested inner owned entity columns, EF Core silently materializes the outer as null while still tracking the inner entity. Any subsequent SaveChanges after replacing the outer owned entity then throws a misleading InvalidOperationException about an identity conflict.

Root cause

IncludeReference only handles the case where the owner entity is non-null. When the owner is null (due to the inconsistent data), the already-tracked inner entity remains in the state manager as an orphan, causing the identity conflict later.

Changes

  • IncludeReference fix (ShaperProcessingExpressionVisitor.ClientMethods.cs): Added an else if branch that detects a null owner with a non-null tracked owned entity on an ownership navigation. The caller detaches the orphaned entry (via the new QueryContext.TryGetEntry) and logs the appropriate warning.

  • QueryContext.TryGetEntry(object entity) (new [EntityFrameworkInternal] method): Looks up a tracked entity by object reference in the state manager. Used by the shaper to retrieve and detach orphaned owned entities.

  • CoreEventId.InconsistentOwnedDataWarning (new event, Warning level): Surfaced via CoreLoggerExtensions.InconsistentOwnedData (non-sensitive) and CoreLoggerExtensions.InconsistentOwnedDataSensitive (includes key values when sensitive data logging is enabled). Default behavior (with WarningBehavior.Throw) will throw on inconsistent data; users can configure to Log or Ignore.

  • API baseline (EFCore.baseline.json): Updated for the new public members.

  • Test (OwnedEntityQueryRelationalTestBase): Verifies that the warning is logged, Outer is null, and SaveChanges succeeds after replacing the owned entity with a valid instance.

// DB row: (Id=X, Outer_RequiredProperty=NULL, Outer_Inner_InnerProperty=42)
var root = await context.Set<RootEntity>().SingleAsync();
// root.Outer == null  ← was silently null before; now logs InconsistentOwnedDataWarning

root.Outer = new Outer { RequiredProperty = 1, Inner = new Inner { InnerProperty = 2 } };
await context.SaveChangesAsync(); // ← previously threw InvalidOperationException (identity conflict)

Copilot AI and others added 2 commits July 9, 2026 02:13
…hanges

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix problem masking and unhelpful exception when loading entity Warn and fix identity conflict when owned entity has inconsistent data in DB Jul 9, 2026
Copilot AI requested a review from AndriySvyryd July 9, 2026 02:15
Comment thread src/EFCore/Query/QueryContext.cs Outdated
Copilot AI and others added 2 commits July 10, 2026 02:12
…sitive logging for InconsistentOwnedData

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…e message generator

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Comment thread src/EFCore/Diagnostics/CoreLoggerExtensions.cs Outdated
…ataWarning to match event ID

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested a review from AndriySvyryd July 10, 2026 07:04
@AndriySvyryd AndriySvyryd marked this pull request as ready for review July 11, 2026 03:39
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner July 11, 2026 03:39
Copilot AI review requested due to automatic review settings July 11, 2026 03:39
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.

Problem masking & unhelpful exception when loading entity with inconsistent data in owned entity columns

2 participants