Skip to content

Fix FK to entity-split principal referencing wrong (fragment) table - #38586

Merged
AndriySvyryd merged 6 commits into
mainfrom
copilot/fix-foreign-key-issue-entity-splitting
Jul 9, 2026
Merged

Fix FK to entity-split principal referencing wrong (fragment) table#38586
AndriySvyryd merged 6 commits into
mainfrom
copilot/fix-foreign-key-issue-entity-splitting

Conversation

CopilotAI commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Fixes#29915

When using SplitToTable, foreign keys from external entities to the split entity incorrectly referenced the last-added fragment table instead of the main table — e.g., FK_Management_Address_CompanyId instead of FK_Management_Company_CompanyId.

modelBuilder.Entity<Company>(e =>e.SplitToTable("Address", t =>{t.Property(x =>x.City);}));// FK on Management was generated pointing to Address instead of CompanymodelBuilder.Entity<Management>(e =>e.HasOne(d =>d.Company).WithMany(p =>p.ManagementSet).HasForeignKey(d =>d.CompanyId));

Root cause

In RelationalModel.PopulateForeignKeyConstraints, principal table mappings are iterated in reverse to prefer the most-derived table for TPT inheritance. For entity-split entities, tableMappings after AddTables is [main_table, fragment_table, ...], so reversing makes the fragment table the first candidate — and since a valid constraint name can be computed against any table that contains the PK column, the FK was created against the fragment table.

An additional consequence of the original fix (filtering out all fragment mappings) was that FKs targeting an alternate key defined on a fragment table were silently dropped, since the AK column only exists on the fragment.

Fix

  • RelationalModel.PopulateForeignKeyConstraints: Non-fragment table mappings (IsSplitEntityTypePrincipal != false) are tried first (reversed, to preserve TPT inheritance preference). Fragment table mappings (IsSplitEntityTypePrincipal == false) are used as a fallback so that FKs targeting an AK that lives on a fragment are still resolved correctly. Non-split and TPT mappings all have IsSplitEntityTypePrincipal = null, so behavior for those cases is unchanged.
  • MigrationsModelDifferTest: Added Foreign_key_to_entity_split_principal_points_to_main_table test covering the PK-targeting FK scenario, and Foreign_key_to_entity_split_principal_ak_on_fragment_points_to_fragment_table test covering the case where the FK targets an AK defined on a fragment table.

When using entity splitting (SplitToTable), a foreign key from an
external entity to the split entity was incorrectly pointing to the
last-added fragment table instead of the main table.
The fix filters out fragment table mappings (IsSplitEntityTypePrincipal
== false) when searching for the principal table to create the FK
constraint, ensuring FKs always reference the main table of the
principal entity.
Fixes#29915
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
CopilotAI changed the title [WIP] Fix foreign key to the wrong table in Entity Framework CoreFix FK to entity-split principal referencing wrong (fragment) tableJul 9, 2026
CopilotAI requested a review from AndriySvyrydJuly 9, 2026 01:23
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review July 9, 2026 02:41
@AndriySvyryd
AndriySvyryd requested a review from a team as a code ownerJuly 9, 2026 02:41
CopilotAI review requested due to automatic review settings July 9, 2026 02:41

CopilotAI 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

Fixes relational model FK constraint resolution for entity-split principals (SplitToTable) so that foreign keys from other entities (and the split fragment FK itself) target the principal’s main table rather than an arbitrary fragment table.

Changes:

  • Filter principal table mappings in RelationalModel.PopulateForeignKeyConstraints to exclude split-fragment mappings (IsSplitEntityTypePrincipal == false) before applying the existing “prefer most-derived” reverse ordering.
  • Add a regression test ensuring FKs to an entity-split principal point to the main table (not the fragment table).

Reviewed changes

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

FileDescription
src/EFCore.Relational/Metadata/Internal/RelationalModel.csEnsures FK principal table selection ignores split fragment mappings while preserving TPT/TPC behavior.
test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.csAdds coverage for the reported FK-to-fragment regression scenario.

…raints to fall back to fragment tables
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>

CopilotAI 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 2 out of 2 changed files in this pull request and generated 1 comment.

Comment threadsrc/EFCore.Relational/Metadata/Internal/RelationalModel.cs Outdated
CopilotAI review requested due to automatic review settings July 9, 2026 04:58
Comment threadsrc/EFCore.Relational/Metadata/Internal/RelationalModel.cs Outdated

CopilotAI 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 2 out of 2 changed files in this pull request and generated 1 comment.

Comment threadsrc/EFCore.Relational/Metadata/Internal/RelationalModel.cs Outdated
CopilotAI requested review from Copilot and removed request for CopilotJuly 9, 2026 05:00
Comment threadsrc/EFCore.Relational/Metadata/Internal/RelationalModel.cs Outdated
CopilotAI review requested due to automatic review settings July 9, 2026 05:02
CopilotAI reviewed Jul 9, 2026

CopilotAI 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@AndriySvyryd
AndriySvyryd merged commit 9b372a7 into mainJul 9, 2026
15 of 16 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/fix-foreign-key-issue-entity-splitting branch July 9, 2026 16:38
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 13, 2026
Sign up for freeto 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.

Entity Framework Core generates a Foreign Key to the wrong table when using Entity Splitting

4 participants

@cincuranet@AndriySvyryd