Skip to content

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

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

Warn and fix identity conflict when owned entity has inconsistent data in DB#38596
AndriySvyryd with Copilot wants to merge 3 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, then calls IgnoreOrphanedOwnedEntity to detach the orphan.

  • QueryContext.IgnoreOrphanedOwnedEntity (new public method): Detaches the orphaned entity from the state manager and logs the warning.

  • CoreEventId.InconsistentOwnedDataWarning (new event, Warning level): Surfaced via CoreLoggerExtensions.InconsistentOwnedData. 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 three 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
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