Skip to content

FIX Insert order bug when using TPC - Issue #35978 #38070

Open
andrewraper-Sage wants to merge 2 commits intodotnet:mainfrom
andrewraper-Sage:tpc-insert-fix
Open

FIX Insert order bug when using TPC - Issue #35978 #38070
andrewraper-Sage wants to merge 2 commits intodotnet:mainfrom
andrewraper-Sage:tpc-insert-fix

Conversation

@andrewraper-Sage
Copy link
Copy Markdown

When using TPC inheritance with abstract base types, SaveChanges produces an incorrect INSERT order - child entities are inserted before their parents, causing FK constraint violations.

This happens because CommandBatchPreparer.AddForeignKeyEdges() fails to create dependency edges for FKs whose principal is an abstract TPC type mapped to no table. Since no IForeignKeyConstraint is created for such FKs, the table-level constraint path finds nothing.

The model-level FK fallback path should handle this, but for INSERT ordering it was skipped by a guard that assumed table-level constraints would cover it, and for DELETE ordering it was gated behind command.Table == null (unreachable for concrete TPC entities which always have a table).

The fix ensures the model-level FK path participates in dependency edge creation when no mapped table-level constraint exists: an additional foreignKey.GetMappedConstraints().Any() check on the INSERT principal registration guard, and removing the else gates on both DELETE paths so they always run alongside the table-level path (CanCreateDependency already prevents double-counting for FKs that do have mapped constraints).

Fixes #35978

@andrewraper-Sage
Copy link
Copy Markdown
Author

@andrewraper-Sage please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree company="Sage"

@andrewraper-Sage andrewraper-Sage marked this pull request as ready for review April 9, 2026 12:25
@andrewraper-Sage andrewraper-Sage requested a review from a team as a code owner April 9, 2026 12:25
Copilot AI review requested due to automatic review settings April 9, 2026 12:25
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes incorrect SaveChanges command ordering for TPC inheritance when a foreign key’s principal is an abstract TPC type mapped to no table (preventing FK constraint violations by ensuring principals are ordered before dependents on INSERT, and dependents before principals on DELETE).

Changes:

  • Update CommandBatchPreparer.AddForeignKeyEdges() to include the model-level FK dependency path when no mapped table-level FK constraints exist.
  • Remove else gating so model-level FK dependency detection runs alongside the table-level constraint path (with CanCreateDependency preventing duplicates).
  • Add regression tests covering INSERT and DELETE ordering for the abstract-TPC-principal scenario.

Reviewed changes

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

File Description
src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs Ensures dependency edges are created via model-level FK logic when table-level constraints are missing, fixing ordering for abstract TPC principals.
test/EFCore.Relational.Tests/Update/CommandBatchPreparerTest.cs Adds regression tests validating correct batching/sort order for Added and Deleted entities under abstract-principal TPC FKs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EF Core Issue: Incorrect Save Order with TPC Inheritance

3 participants