Warn and fix identity conflict when owned entity has inconsistent data in DB#38596
Draft
AndriySvyryd with Copilot wants to merge 3 commits into
Draft
Warn and fix identity conflict when owned entity has inconsistent data in DB#38596AndriySvyryd with Copilot wants to merge 3 commits into
AndriySvyryd with Copilot wants to merge 3 commits into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When a row has
NULLin a non-nullable column of an outer owned entity but non-NULLvalues in nested inner owned entity columns, EF Core silently materializes the outer asnullwhile still tracking the inner entity. Any subsequentSaveChangesafter replacing the outer owned entity then throws a misleadingInvalidOperationExceptionabout an identity conflict.Root cause
IncludeReferenceonly handles the case where the owner entity is non-null. When the owner isnull(due to the inconsistent data), the already-tracked inner entity remains in the state manager as an orphan, causing the identity conflict later.Changes
IncludeReferencefix (ShaperProcessingExpressionVisitor.ClientMethods.cs): Added anelse ifbranch that detects a null owner with a non-null tracked owned entity on an ownership navigation, then callsIgnoreOrphanedOwnedEntityto detach the orphan.QueryContext.IgnoreOrphanedOwnedEntity(new public method): Detaches the orphaned entity from the state manager and logs the warning.CoreEventId.InconsistentOwnedDataWarning(new event,Warninglevel): Surfaced viaCoreLoggerExtensions.InconsistentOwnedData. Default behavior (withWarningBehavior.Throw) will throw on inconsistent data; users can configure toLogorIgnore.API baseline (
EFCore.baseline.json): Updated for the three new public members.Test (
OwnedEntityQueryRelationalTestBase): Verifies that the warning is logged,Outerisnull, andSaveChangessucceeds after replacing the owned entity with a valid instance.