Skip to content

Add validation for Index included properties inside JSON-mapped types#38404

Open
AndriySvyryd wants to merge 4 commits into
mainfrom
ValidateIndexInclude
Open

Add validation for Index included properties inside JSON-mapped types#38404
AndriySvyryd wants to merge 4 commits into
mainfrom
ValidateIndexInclude

Conversation

@AndriySvyryd

@AndriySvyryd AndriySvyryd commented Jun 10, 2026

Copy link
Copy Markdown
Member

Adds SQL Server-specific model validation to prevent configuring index INCLUDE properties that are nested inside a JSON-mapped type, and updates the annotation provider to correctly resolve JSON complex property paths to their container column names.

Changes

  • SqlServerModelValidator: Replace the "traverses a complex collection" check with a broader check on propertyBase.DeclaringType.IsMappedToJson(). This correctly rejects any include path whose target property lives inside a JSON-mapped type (collection or non-collection), while still allowing the JSON column itself to be included directly.
  • SqlServerAnnotationProvider: Update include-column resolution to use FindPropertyBaseByPath and handle complex property paths by returning the JSON container column name via ComplexType.GetContainerColumnName().
  • SqlServerStrings: Replace IncludePropertyTraversesComplexCollection resource with IncludePropertyInJsonMappedType (3 parameters, no complexCollection argument).
  • Tests: Update and expand model-validation tests to cover: nested property inside a JSON collection (throws), nested property inside a non-collection JSON complex type (throws), and including a JSON complex property/collection directly (valid).

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

Adds SQL Server-specific model validation to prevent configuring index included properties that traverse complex collections (JSON-mapped), and expands test coverage to lock in the expected behavior and SQL generation for complex-property includes.

Changes:

  • Add SqlServerModelValidator validation that throws when an index include path traverses a complex collection.
  • Add a new provider error message resource (IncludePropertyTraversesComplexCollection).
  • Add/extend tests: model-validation coverage for the new error, and a migrations functional test verifying INCLUDE column name generation for a complex property path.

Reviewed changes

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

Show a summary per file
File Description
test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs Adds a regression test asserting an exception when an index include path traverses a complex collection.
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs Adds a functional test ensuring complex-property include paths translate to the expected included column name in generated SQL.
src/EFCore.SqlServer/Properties/SqlServerStrings.resx Adds a new localized error message for invalid include paths over complex collections.
src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs Adds the strongly-typed accessor for the new resource string.
src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs Implements the new include-property validation logic and helper for detecting complex-collection traversal.
Files not reviewed (1)
  • src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs: Language not supported

Comment thread src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…SON-mapped type

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title Add validation for Index included complex collection properties Add validation for Index included properties inside JSON-mapped types Jun 10, 2026
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 10, 2026 22:09
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 10, 2026 22:09
Copilot AI review requested due to automatic review settings June 10, 2026 22:09
@AndriySvyryd AndriySvyryd requested a review from cincuranet June 10, 2026 22:09

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 5 out of 6 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs: Language not supported

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
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.

3 participants