Skip to content

Throw for multiple owned entities set to the same instance - #38446

Merged
AndriySvyryd merged 15 commits into
mainfrom
copilot/fix-multiple-owned-entities
Jun 19, 2026
Merged

AndriySvyryd merged 15 commits into
mainfrom
copilot/fix-multiple-owned-entities

Conversation

Copilot AI commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

When the same object instance is assigned to multiple owned navigations, EF Core produced a confusing "unknown FK value" error at SaveChanges time. This now throws a clear InvalidOperationException at tracking time with an actionable message.

This detection covers all scenarios where the same owned instance is referenced multiple times:

  1. Same owner, different navigations: Same instance assigned to two different owned navigations on one entity
  2. Already tracked, different owner: An already-tracked owned entity is assigned to a navigation on a different owner
  3. Already tracked, same owner but different navigation: An already-tracked owned entity is assigned to a different navigation on the same owner (detected via DetectChanges)
  4. Different owners, same navigation: Same instance assigned to the same navigation on two different owners
// Scenario 1: Same owner, different navigations
var owned = new Owned(1, 2, 3, 4);
var parent = new Owner
{
    First = owned,  // same instance
    Last = owned    // same instance — now throws immediately
};
context.Add(parent); // throws InvalidOperationException

// Scenario 3: Already tracked, same owner, different navigation (via DetectChanges)
context.Add(parent); // parent.First = owned, tracked
parent.Last = owned; // same instance on a different navigation
context.ChangeTracker.DetectChanges(); // throws InvalidOperationException

// Scenario 4: Different owners, same navigation
var owner1 = new Owner { Id = 1, Child = owned };
var owner2 = new Owner { Id = 2, Child = owned };
context.Add(owner1);
context.Add(owner2); // throws InvalidOperationException

Changes

  • NavigationFixer.InitialFixup: Added check when an already-tracked owned entity is found during graph traversal to detect if it belongs to a different owner. This check only runs when the owner entry is being Added (EntityState.Added) to avoid interfering with legitimate ownership transfers during DetectChanges.
  • NavigationFixer.InitialFixup (danglers resolution): Added check when processing recorded referrers (danglers) for an untracked entity that's now being tracked, to detect if multiple non-collection owned navigations from the same or different owners reference it.
  • NavigationFixer.NavigationReferenceChanged: Added check for the shared CLR type scenario where an owned entity already tracked under one entity type is assigned to a navigation pointing to a different entity type. Only throws when the existing owner's navigation still references the entity (genuine duplicate); legitimate move/swap operations where the original navigation has already been cleared are allowed through.
  • CoreStrings.resx: Added DuplicateOwnedEntityInstance resource string showing both navigations and both owner types (5 parameters).
  • CoreStrings.Designer.cs and EFCore.baseline.json: Updated for the new 5-parameter format.
  • OwnedFixupTest: Added five tests covering all duplicate owned entity detection scenarios:
    • Same owner, both navigations set upfront
    • Already-tracked entity, different owner (Added state)
    • Different owners, same navigation
    • Already-tracked entity, same owner, different navigation — detected via DetectChanges
    • Already-tracked entity, different owner — existing entity in Unchanged state

… navigations

Detect when the same object instance is referenced from multiple owned navigation
properties on the same entity during graph traversal, and throw a clear
InvalidOperationException with an actionable error message instead of the confusing
"unknown FK value" error that occurred during SaveChanges.

Closes #24614

Co-authored-by: AndriySvyryd <[email protected]>
Copilot AI changed the title [WIP] Fix exception for same instance of owned entities Throw by default for multiple owned entities set to the same instance Jun 17, 2026
Copilot AI requested a review from AndriySvyryd June 17, 2026 23:06
@AndriySvyryd
AndriySvyryd requested a review from Copilot June 17, 2026 23:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves change-tracking diagnostics by detecting when the same CLR instance is assigned to multiple reference owned navigations on the same owner and throwing an actionable InvalidOperationException during graph tracking (instead of surfacing a confusing FK/unknown-key error later at SaveChanges time).

Changes:

  • Add duplicate-owned-instance detection during graph traversal in EntityEntryGraphIterator (sync + async paths).
  • Introduce a new CoreStrings.DuplicateOwnedEntityInstance resource and corresponding generated accessor.
  • Add a regression test validating the new throw-at-tracking behavior.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
test/EFCore.Tests/ChangeTracking/Internal/OwnedFixupTest.cs Adds a test asserting tracking throws when the same owned instance is assigned to two owned reference navigations.
src/EFCore/Properties/CoreStrings.resx Adds the new localized error message for duplicate owned instance detection.
src/EFCore/Properties/CoreStrings.Designer.cs Adds the generated strongly-typed accessor for the new resource string.
src/EFCore/ChangeTracking/Internal/EntityEntryGraphIterator.cs Adds the duplicate-owned-instance check before node handling in graph traversal.
Files not reviewed (1)
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file

Copilot AI and others added 3 commits June 18, 2026 00:39
- Check when owned entity is already tracked (different owner or same owner/different nav)
- Check when different owners reference same owned instance from same navigation
- Update error message to show both navigations and owners
- Add tests for new scenarios

Co-authored-by: AndriySvyryd <[email protected]>
Copilot AI review requested due to automatic review settings June 18, 2026 02:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file

Copilot AI requested review from Copilot and removed request for Copilot June 18, 2026 04:11
Copilot AI requested review from Copilot and removed request for Copilot June 18, 2026 04:19
@AndriySvyryd
AndriySvyryd requested a review from cincuranet June 18, 2026 04:22
@AndriySvyryd AndriySvyryd changed the title Throw by default for multiple owned entities set to the same instance Throw for multiple owned entities set to the same instance Jun 19, 2026
@AndriySvyryd
AndriySvyryd merged commit 2c99b9a into main Jun 19, 2026
13 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/fix-multiple-owned-entities branch June 19, 2026 17:15
@github-actions github-actions Bot added the api-review This PR or issue is introducing public API changes that need to be reviewed label Jun 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

API review baseline changes for src/EFCore/EFCore.baseline.json

Show diff

The diff below was generated by ApiChief between the base and the PR.

  static class Microsoft.EntityFrameworkCore.Diagnostics.CoreStrings
+ static string DuplicateOwnedEntityInstance(object? entityType, object? firstNavigation, object? firstOwnerType, object? secondNavigation,
+     object? secondOwnerType);

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.

4 participants