Investigation: remaining model-building items require non-surgical reworks#38423
Draft
Copilot wants to merge 1 commit into
Draft
Investigation: remaining model-building items require non-surgical reworks#38423Copilot wants to merge 1 commit into
Copilot wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
HasForeignKeysets FK properties after creating FKInternalEntityTypeBuilder.HasForeignKey(private, ~L2612) creates the relationship with conventional shadow properties viaHasRelationshipInternal, then sets the real properties in a second pass.Threading
dependentPropertiesintoHasRelationshipInternal→CreateForeignKey(single-step creation) breaks 7 tests inInternalEntityTypeBuilderTest/InternalForeignKeyBuilderTest:Check.DebugAssert failed: FK not found—SetOrAddForeignKeyasserts instead of matching a pre-existing FK in the hierarchy.{TempId, TempId1}instead of reusing existing{Id, Unique}.The richer
InternalForeignKeyBuilder.HasForeignKeypath 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.Attachreceive only the dependent type and re-resolve the principal heuristically (InternalForeignKeyBuilder.cs~L3499–3540). Threading the principal through widensAttachacross 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
SetOrAddForeignKey/CreateForeignKeyabsorb the FK matching/promotion currently inInternalForeignKeyBuilder.HasForeignKey?