Fix ArgumentNullException when a foreign key references a key declared on a complex type property - #38764
Open
AndriySvyryd with Copilot wants to merge 4 commits into
Open
Fix ArgumentNullException when a foreign key references a key declared on a complex type property#38764AndriySvyryd with Copilot wants to merge 4 commits into
AndriySvyryd with Copilot wants to merge 4 commits into
Conversation
…imeModelConvention Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix ArgumentNullException in RuntimeModelConvention for foreign key
Fix ArgumentNullException when a foreign key references a key declared on a complex type property
Aug 7, 2026
There was a problem hiding this comment.
Pull request overview
Fixes a model-finalization crash in RuntimeModelConvention when a relationship references a principal key whose properties are declared within a complex type (e.g., Quarks.Up). This aligns runtime-model lookup behavior with existing creation logic that already understands complex property chains.
Changes:
- Updated
RuntimeModelConventionto resolve key/FK/index property lists viaFindRuntimeProperty/FindRuntimePropertyBase(complex-chain aware) instead of name-onlyFindProperties. - Applied the same lookup fix across
GetKey,GetForeignKey,GetIndex, andCreate(IForeignKey)to avoid null property resolution leading toArgumentNullException. - Added a specification test covering an alternate key on a complex property used as a principal key in a relationship.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/EFCore/Metadata/Conventions/RuntimeModelConvention.cs | Switches runtime-model property resolution to complex-chain-aware helpers for keys, foreign keys, and unnamed indexes. |
| test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.ComplexType.cs | Adds a regression test reproducing the foreign-key-to-complex-hosted-key finalization failure. |
AndriySvyryd
reviewed
Aug 7, 2026
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
AndriySvyryd
marked this pull request as ready for review
August 7, 2026 23:14
AndriySvyryd
enabled auto-merge (squash)
August 7, 2026 23:15
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.
Defining a key on a scalar nested inside a complex type works, but as soon as any relationship uses it as its principal key, model finalization throws
ArgumentNullException: Value cannot be null. (Parameter 'key').RuntimeModelConvention.GetKeyresolved the principal key's properties viaRuntimeEntityType.FindProperties(names), which only sees properties declared directly on the entity type. For a complex-hosted key the lookup returnsnull, andRuntimeEntityType.FindKey(null)throws. This affects both runtime (context.Model) and design time (Add-Migration), so there is no workaround.Changes
RuntimeModelConvention— replaced the name-basedFindPropertieslookups with the existingFindRuntimeProperty/FindRuntimePropertyBasehelpers, which walk the complex property chain from the entity type down to the declaring complex type. Applied toGetKey,GetForeignKey,GetIndex, andCreate(IForeignKey). These helpers were already used byCreate(IKey)andCreate(IIndex), so this aligns the lookup side with the creation side.Can_use_alternate_key_on_complex_property_as_principal_keyinModelBuilderTest.ComplexType.csdefines an alternate key on a complex type property and a relationship using it as the principal key; it reproduces the reported exception without the fix.