Warn and fix identity conflict when owned entity has inconsistent data in DB#38596
Draft
AndriySvyryd with Copilot wants to merge 6 commits into
Draft
Warn and fix identity conflict when owned entity has inconsistent data in DB#38596AndriySvyryd with Copilot wants to merge 6 commits into
AndriySvyryd with Copilot wants to merge 6 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
…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>
| /// </summary> | ||
| /// <param name="diagnostics">The diagnostics logger to use.</param> | ||
| /// <param name="navigation">The ownership navigation.</param> | ||
| public static void InconsistentOwnedData( |
Member
There was a problem hiding this comment.
@copilot Make sure Every_eventId_has_a_logger_method_and_logs_when_level_enabled isn't failing
Contributor
Author
There was a problem hiding this comment.
Fixed in commit fa1e28c:
- Renamed
InconsistentOwnedData→InconsistentOwnedDataWarninginCoreLoggerExtensions.csso the method name matches theCoreEventId.InconsistentOwnedDataWarningfield name (which the test looks up by reflection). - Updated
FakeNavigationBase.TargetEntityTypeinCoreEventIdTest.csto return(IEntityType)DeclaringTypeinstead of throwingNotImplementedException(the method now callsnavigation.TargetEntityType.DisplayName()). - Updated the caller in
ClientMethods.csand regenerated the API baseline.
…ataWarning to match event ID Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
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. The caller detaches the orphaned entry (via the newQueryContext.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,Warninglevel): Surfaced viaCoreLoggerExtensions.InconsistentOwnedData(non-sensitive) andCoreLoggerExtensions.InconsistentOwnedDataSensitive(includes key values when sensitive data logging is enabled). Default behavior (withWarningBehavior.Throw) will throw on inconsistent data; users can configure toLogorIgnore.API baseline (
EFCore.baseline.json): Updated for the new public members.Test (
OwnedEntityQueryRelationalTestBase): Verifies that the warning is logged,Outerisnull, andSaveChangessucceeds after replacing the owned entity with a valid instance.