Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix FK dependency ordering when replacing owned entities with foreign key references#36769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Copilot wants to merge3 commits intorelease/10.0
base:release/10.0
Choose a base branch
Loading
fromcopilot/fix-36059

Conversation

Copy link
Contributor

CopilotAI commentedSep 12, 2025
edited
Loading

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 (likeFile) references another entity (likeContent) through a foreign key, and the owned entity is replaced duringSaveChanges(), EF Core was generating commands in this incorrect order:

DELETEFROM [Contents]WHERE [Id]= @p0;-- ❌ Deletes referenced entity firstINSERT INTO [Contents] ([Id], [Data])VALUES (@p1, @p2);UPDATE [Documents]SET [File_ContentId]= @p3WHERE [Id]= @p6;

This causes a foreign key constraint violation because theUPDATE command tries to referenceContent(Id=2) before it's inserted, and theDELETE command tries to removeContent(Id=1) while it's still being referenced.

Solution

The fix modifies theCanCreateDependency method inCommandBatchPreparer 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 firstUPDATE [Documents]SET [File_ContentId]= @p3WHERE [Id]= @p6;DELETEFROM [Contents]WHERE [Id]= @p0;-- ✅ Delete old content last

Changes Made

  • EnhancedCanCreateDependency 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 caseBatchCommands_sorts_FK_dependencies_correctly_when_replacing_owned_entity to verify the fix

Test Case

The fix handles scenarios like:

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

Addressing#36059.


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

CopilotAIand others added2 commitsSeptember 12, 2025 23:38
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…refinementCo-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
CopilotAI changed the title[WIP] FK dependency ordering wrong when replacing an inline owned entityFix FK dependency ordering when replacing owned entities with foreign key referencesSep 12, 2025
Copilot finished work on behalf ofAndriySvyrydSeptember 12, 2025 23:52
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@AndriySvyrydAndriySvyrydAwaiting requested review from AndriySvyryd

At least 1 approving review is required to merge this pull request.

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@AndriySvyryd

[8]ページ先頭

©2009-2025 Movatter.jp