Skip to content

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
mainfrom
copilot/resolve-argumentnullexception-issue
Open

Fix ArgumentNullException when a foreign key references a key declared on a complex type property#38764
AndriySvyryd with Copilot wants to merge 4 commits into
mainfrom
copilot/resolve-argumentnullexception-issue

Conversation

Copilot AI commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

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').

parent.ComplexProperty(p => p.Id);
parent.HasKey(p => p.Id.Value);          // key declared on the complex type

parent.HasMany(p => p.Children).WithOne() // trigger: FK resolving that key
      .HasForeignKey("ParentId");

RuntimeModelConvention.GetKey resolved the principal key's properties via RuntimeEntityType.FindProperties(names), which only sees properties declared directly on the entity type. For a complex-hosted key the lookup returns null, and RuntimeEntityType.FindKey(null) throws. This affects both runtime (context.Model) and design time (Add-Migration), so there is no workaround.

Changes

  • RuntimeModelConvention — replaced the name-based FindProperties lookups with the existing FindRuntimeProperty / FindRuntimePropertyBase helpers, which walk the complex property chain from the entity type down to the declaring complex type. Applied to GetKey, GetForeignKey, GetIndex, and Create(IForeignKey). These helpers were already used by Create(IKey) and Create(IIndex), so this aligns the lookup side with the creation side.
  • TestCan_use_alternate_key_on_complex_property_as_principal_key in ModelBuilderTest.ComplexType.cs defines 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.

Copilot AI and others added 2 commits August 7, 2026 20:41
…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
Copilot AI requested a review from AndriySvyryd August 7, 2026 20:51
@AndriySvyryd
AndriySvyryd requested a lite review from Copilot August 7, 2026 21:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 RuntimeModelConvention to resolve key/FK/index property lists via FindRuntimeProperty / FindRuntimePropertyBase (complex-chain aware) instead of name-only FindProperties.
  • Applied the same lookup fix across GetKey, GetForeignKey, GetIndex, and Create(IForeignKey) to avoid null property resolution leading to ArgumentNullException.
  • 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.

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested a review from AndriySvyryd August 7, 2026 22:26
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review August 7, 2026 23:14
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner August 7, 2026 23:14
Copilot AI review requested due to automatic review settings August 7, 2026 23:14
@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) August 7, 2026 23:15
@AndriySvyryd
AndriySvyryd requested a review from cincuranet August 7, 2026 23:15

Copilot AI left a comment

Copy link
Copy Markdown

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 no new comments.

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.

ArgumentNullException from RuntimeModelConvention when a foreign key references a principal key defined on a complex type property (11.0.0-preview.6)

3 participants