Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 12, 2025

This PR addresses a critical issue where Entity Framework Core generates SQL commands in the wrong order when replacing an owned entity that has foreign key relationships to other entities, causing FK constraint violations.

Problem

When an owned entity (like File) references another entity (like Content) through a foreign key, and the owned entity is replaced during SaveChanges(), EF Core was generating commands in this incorrect order:

DELETE FROM [Contents] WHERE [Id] = @p0;           -- ❌ Deletes referenced entity first
INSERT INTO [Contents] ([Id], [Data]) VALUES (@p1, @p2);
UPDATE [Documents] SET [File_ContentId] = @p3 WHERE [Id] = @p6;

This causes a foreign key constraint violation because the UPDATE command tries to reference Content(Id=2) before it's inserted, and the DELETE command tries to remove Content(Id=1) while it's still being referenced.

Solution

The fix modifies the CanCreateDependency method in CommandBatchPreparer to properly handle foreign key dependencies for owned entities. Previously, the dependency tracking logic was excluding owned entity FK relationships under the assumption they were "handled elsewhere," but this was incorrect for owned entities that reference non-owned entities.

The corrected command order is:

INSERT INTO [Contents] ([Id], [Data]) VALUES (@p1, @p2);  -- ✅ Insert new content first
UPDATE [Documents] SET [File_ContentId] = @p3 WHERE [Id] = @p6;
DELETE FROM [Contents] WHERE [Id] = @p0;                  -- ✅ Delete old content last

Changes Made

  • Enhanced CanCreateDependency to identify owned entities that have FK relationships to non-owned entities
  • Allow dependency creation for these relationships even when FK constraints exist at the table level
  • Added comprehensive test case BatchCommands_sorts_FK_dependencies_correctly_when_replacing_owned_entity to verify the fix

Test Case

The fix handles scenarios like:

// Document owns File, File references Content
document.File = new File { ContentId = 2, Content = newContent };  // Replace owned entity
context.Remove(oldContent);  // Remove old referenced entity
context.SaveChanges();  // Now generates correct SQL order

Addressing #36059.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] FK dependency ordering wrong when replacing an inline owned entity Fix FK dependency ordering when replacing owned entities with foreign key references Sep 12, 2025
@Copilot Copilot AI requested a review from AndriySvyryd September 12, 2025 23:52
Copilot finished work on behalf of AndriySvyryd September 12, 2025 23:52
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