Skip to content

Make model snapshots with mismatched FK/PK property types usable - #38450

Merged
AndriySvyryd merged 13 commits into
mainfrom
copilot/allow-fks-different-type-from-pks
Jun 19, 2026
Merged

AndriySvyryd merged 13 commits into
mainfrom
copilot/allow-fks-different-type-from-pks

Conversation

Copilot AI commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #30827

Per maintainer feedback, this PR no longer adds foreign key/principal key type enforcement in ModelValidator. Instead, it focuses on snapshot compatibility so existing snapshots with mismatched key/foreign-key property CLR types can still build a usable model.

Changes

  • Removed redundant core validation change: reverted the ModelValidator.ValidateForeignKeys check and its invocation.
  • Snapshot compatibility fix (model-building): added compatibility in model building so legacy snapshots that contain mismatched FK/PK property CLR types still produce a usable model.
  • Snapshot detection cleanup: renamed/moved snapshot detection to Model.IsInModelSnapshot, and updated compatibility checks to use it.
  • Tests:
    • Added a snapshot regression test: Snapshot_with_mismatched_key_and_foreign_key_property_types_is_usable.
    • Kept relational test expectations reverted to their prior behavior.

Scope

This PR is now strictly about model snapshot compatibility.
It does not introduce new runtime FK/PK type validation in ModelValidator.

Copilot AI changed the title [WIP] Fix foreign keys with different types from primary keys Validate that foreign key property types match principal key property types Jun 17, 2026
Copilot AI requested a review from AndriySvyryd June 17, 2026 23:42
@AndriySvyryd
AndriySvyryd requested a review from Copilot June 17, 2026 23:43

Copilot AI 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

Adds core model validation to reject foreign keys whose provider CLR types don’t match the referenced principal key’s provider CLR types, aligning runtime behavior with maintainer guidance while continuing to implicitly skip validation for model snapshots.

Changes:

  • Introduces a new ModelValidator.ValidateForeignKeys pass that compares dependent vs. principal provider CLR types and throws CoreStrings.ForeignKeyTypeMismatch on mismatches.
  • Adds/updates tests to cover matching vs. mismatched provider-type scenarios and updates relational tests impacted by earlier validation failure.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/EFCore/Infrastructure/ModelValidator.cs Adds provider-type FK validation during model validation.
test/EFCore.Tests/Infrastructure/ModelValidatorTest.cs Adds unit tests for matching/mismatched provider-type FK validation.
test/EFCore.Relational.Tests/Storage/RelationalTypeMappingTest.cs Updates a relational mapping test to expect the new FK provider-type validation failure.
test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.cs Updates a relational validator test impacted by the new earlier FK validation behavior.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@AndriySvyryd AndriySvyryd changed the title Validate that foreign key property types match principal key property types Allow foreign key property types to not match principal key property types exactly in snapshots Jun 18, 2026
Copilot AI changed the title Allow foreign key property types to not match principal key property types exactly in snapshots Make model snapshots with mismatched FK/PK property types usable Jun 18, 2026
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review June 18, 2026 02:41
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner June 18, 2026 02:41

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

Comment thread src/EFCore/Metadata/Internal/Model.cs Resolved
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 18, 2026 04:23

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

Comment thread src/EFCore/Metadata/Internal/Model.cs Resolved
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 18, 2026 04:36

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

Comment thread src/EFCore/Metadata/Internal/Model.cs Resolved
@AndriySvyryd
AndriySvyryd merged commit 7fe7560 into main Jun 19, 2026
14 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/allow-fks-different-type-from-pks branch June 19, 2026 17:17
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 23, 2026
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.

Allow FKs to have a different type from PKs in the snapshot

4 participants