Skip to content

Fix FK dependency ordering for owned entities with external FK references (WIP)#37694

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/port-efcore-pr-36769-to-main
Draft

Fix FK dependency ordering for owned entities with external FK references (WIP)#37694
Copilot wants to merge 4 commits intomainfrom
copilot/port-efcore-pr-36769-to-main

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

Addresses issue #36059 where FK dependency ordering is incorrect when replacing an owned entity that has a foreign key to a non-owner entity, causing FK constraint violations during SaveChanges.

Problem

When replacing an owned entity that has a foreign key to a non-owner entity, SaveChanges generates commands in incorrect order, causing FK constraint violations. The DELETE of old referenced data executes before the UPDATE that switches the reference.

Example scenario from issue #36059:

  • Document owns File (table-split)
  • File has FK to Content
  • When replacing File: Current order is DELETE Content1, UPDATE Document, INSERT Content2
  • Expected order: INSERT Content2, UPDATE Document, DELETE Content1

Current Status

⚠️ Work in Progress - Investigation ongoing

CommandBatchPreparerTest.cs

CommandBatchPreparer.cs

Investigation Findings

The issue occurs because:

  1. For owned entities with table-splitting, FK properties are shadow properties on the owner
  2. When owned entities are replaced, change tracking may not mark FK properties as modified
  3. IsModified(foreignKey.Columns, command) returns false at line 768, skipping dependency creation
  4. Without the dependency, commands are ordered incorrectly

A new fix is being developed that handles FK dependencies for owned entities even when change tracking doesn't detect modifications.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Port changes from PR #36769 to main branch Port PR #36769: Fix FK dependency ordering for owned entities with external FK references Feb 13, 2026
Copilot AI requested a review from AndriySvyryd February 13, 2026 05:07
}

[ConditionalFact]
public void BatchCommands_sorts_FK_dependencies_correctly_when_replacing_owned_entity()
Copy link
Member

Choose a reason for hiding this comment

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

@copilot This test passes even without the fix. Modify it so it fails without the fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 7fd2d9c. The test now uses actual owned entities (FileEntity owned by DocumentEntity with an FK to ContentEntity) and properly fails without the fix. However, I discovered that the fix from PR #36769 doesn't make the test pass either, suggesting there may be an issue with the original fix or additional work needed to make it work correctly with the owned entity scenario.

Copy link
Member

Choose a reason for hiding this comment

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

@copilot Ok, then develop a different fix. Note that the original issue was reported at #36059

Copy link
Member

Choose a reason for hiding this comment

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

@copilot Continue

Modified the test to use a proper owned entity (FileEntity) with an FK to a non-owner entity (ContentEntity). The test now properly reproduces issue #36059 and fails without the fix. However, the fix from PR #36769 is not making the test pass, suggesting there may be an issue with the fix itself or additional work needed.

Co-authored-by: AndriySvyryd <[email protected]>
Copilot AI requested a review from AndriySvyryd February 13, 2026 22:44
Copilot stopped work on behalf of AndriySvyryd due to an error February 14, 2026 00:16
Copilot AI changed the title Port PR #36769: Fix FK dependency ordering for owned entities with external FK references Fix FK dependency ordering for owned entities with external FK references (WIP) Feb 14, 2026
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.

2 participants

Comments