Skip to content

Investigation: remaining model-building items require non-surgical reworks#38423

Draft
Copilot wants to merge 1 commit into
mainfrom
copilot/various-model-building-issues
Draft

Investigation: remaining model-building items require non-surgical reworks#38423
Copilot wants to merge 1 commit into
mainfrom
copilot/various-model-building-issues

Conversation

Copilot AI commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Tracking issue with three unchecked items in EF Core's relationship-building engine. I verified all three are still present and tested whether each can be fixed surgically. No code changes are shipped — empirically, the straightforward fixes regress, and the correct fixes need design direction. Findings below.

1. HasForeignKey sets FK properties after creating FK

InternalEntityTypeBuilder.HasForeignKey (private, ~L2612) creates the relationship with conventional shadow properties via HasRelationshipInternal, then sets the real properties in a second pass.

Threading dependentProperties into HasRelationshipInternalCreateForeignKey (single-step creation) breaks 7 tests in InternalEntityTypeBuilderTest/InternalForeignKeyBuilderTest:

  • Check.DebugAssert failed: FK not foundSetOrAddForeignKey asserts instead of matching a pre-existing FK in the hierarchy.
  • principal key resolves to {TempId, TempId1} instead of reusing existing {Id, Unique}.
  • derived FK not promoted (2 FKs instead of 1).

The richer InternalForeignKeyBuilder.HasForeignKey path does FK matching/promotion and principal-key reuse that the creation path lacks. A correct fix moves that logic into creation — a rework, not surgical.

2. Pass in principal type when attaching FK

RelationshipSnapshot.Attach / InternalForeignKeyBuilder.Attach receive only the dependent type and re-resolve the principal heuristically (InternalForeignKeyBuilder.cs ~L3499–3540). Threading the principal through widens Attach across many call sites and transient detach→reattach snapshots; the re-resolution exists for the principal-not-in-model case, so a naive parameter has limited value.

3. Inverting identifying FK to derived moves the FK to base

Invert branch of SetRelatedTypes (InternalForeignKeyBuilder.cs ~L1440–1441): dependentEntityType.LeastDerivedType(Metadata.PrincipalEntityType) walks up to the base. The "moves to base" mechanism reproduces, but in every scenario I could build it matches the intended least-derived placement and the inverted FK is no longer identifying — no unambiguous bug to anchor a regression-safe fix.

Asks for the reviewer

Copilot AI changed the title [WIP] Fix various model building issues in EF Core Investigation: remaining model-building items require non-surgical reworks Jun 13, 2026
Copilot AI requested a review from AndriySvyryd June 13, 2026 00:24
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