Skip to content

[release/11.0] Warn when collection properties are mapped as scalars - #39664

Merged
AndriySvyryd merged 5 commits into
release/11.0from
copilot/fix-translation-error-collection-property
Nov 16, 2026
Merged

AndriySvyryd merged 5 commits into
release/11.0from
copilot/fix-translation-error-collection-property

Conversation

@AndriySvyryd

@AndriySvyryd AndriySvyryd commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #39600

Description
In EF Core 11 Property() was changed to always map a member as a scalar even if it's a collection. This PR adds a warning when a collection-typed property is explicitly configured as a scalar and has no value converter, suggesting PrimitiveCollection() to avoid later query translation failures.

Customer impact
Applications that configure primitive collections with Property() may encounter query translation failures. The workaround is to configure the member with PrimitiveCollection(), however it's not obvious without this warning.

How found
Customer reported.

Regression
The scalar-mapping behavior was introduced in EF Core 11 as an intentional breaking change.

Testing
Tests added

Risk
Very low. The change just adds an ignorable warning.

AndriySvyryd and others added 2 commits November 15, 2026 03:09
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@AndriySvyryd AndriySvyryd changed the title [WIP] Fix unhelpful translation error for collection property in 11 Warn when collection properties are mapped as scalars Nov 15, 2026
@AndriySvyryd
AndriySvyryd requested a lite review from Copilot November 15, 2026 05:54

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.

🟢 Approval recommended

The supplied review evidence identifies no active issues for this change.

No open issues 🎉

1 file not reviewed
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file
What changed in this PR

Adds model-validation diagnostics for collection-typed properties explicitly mapped as scalar properties, helping users identify when Property() should be replaced with PrimitiveCollection().

Changes:

  • Emits CoreEventId.AccidentalScalarCollection for explicitly scalar-mapped collection properties without converters, excluding strings, byte[], and primitive collections.
  • Adds localized warning resources, diagnostic logging integration, property metadata payloads, and public API baseline updates.
  • Adds core and SQL Server tests covering warning behavior, configuration, exclusions, and query translation.
File Description
test/​EFCore.Tests/​Infrastructure/​ModelValidatorTest.cs Tests scalar collection warnings, exclusions, complex and shadow properties, and warning configuration behavior.
test/​EFCore.SqlServer.Tests/​Infrastructure/​SqlServerModelValidatorTest.cs Verifies diagnostics for failed scalar collection query translation and successful primitive collection translation.
src/​EFCore/​Properties/​CoreStrings.resx Adds the localized warning message and formatting metadata.
src/​EFCore/​Properties/​CoreStrings.Designer.cs Adds the generated resource accessor and event definition for the warning.
src/​EFCore/​Infrastructure/​ModelValidator.cs Detects explicitly configured collection properties that are mapped as scalars during model validation.
src/​EFCore/​EFCore.baseline.json Updates the public API baseline for the new diagnostic event and logger extension.
src/​EFCore/​Diagnostics/​LoggingDefinitions.cs Adds storage for the new logging definition.
src/​EFCore/​Diagnostics/​CoreLoggerExtensions.cs Logs the warning and dispatches property metadata through diagnostic events.
src/​EFCore/​Diagnostics/​CoreEventId.cs Defines and documents the new AccidentalScalarCollection event.

Review effort: Lite

@AndriySvyryd AndriySvyryd changed the title Warn when collection properties are mapped as scalars [release/11.0] Warn when collection properties are mapped as scalars Nov 15, 2026
@dotnet dotnet deleted a comment from Copilot AI Nov 15, 2026
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review November 15, 2026 21:14
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner November 15, 2026 21:14
@github-code-quality

github-code-quality Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage

release/11.0 a8c2f29 copilot/fix-translat... 3378ff0 +/-
76% 67% -9%
View coverage of the 10 most impacted files.
File release/11.0 a8c2f29 copilot/fix-translat... 3378ff0 +/-
/Users/runner/w...nal/Snapshot.cs 0% 60% +60%
D:\a\efcore\efc...nal\Snapshot.cs 0% 64% +64%
/home/runner/wo...nal/Snapshot.cs 0% 66% +66%
/home/runner/wo...yTypeBuilder.cs 0% 88% +88%
D:\a\efcore\efc...yTypeBuilder.cs 0% 88% +88%
/Users/runner/w...ngs.Designer.cs 0% 92% +92%
/home/runner/wo...ctExpression.cs 0% 93% +93%
D:\a\efcore\efc...ctExpression.cs 0% 93% +93%
D:\a\efcore\efc...ngs.Designer.cs 0% 94% +94%
/home/runner/wo...ngs.Designer.cs 0% 95% +95%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 16, 2026 19:07 UTC

@artl93 artl93 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Newer, but diagnostics not obvious, customer reported. M2 approved.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings November 16, 2026 17:46

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.

🟡 Changes recommended

Active issues remain: the async sequence check can recommend an unsupported configuration, and non-shared fixtures still throw on existing collection-property mappings.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Exclude unsupported IAsyncEnumerable<T> from primitive-collection warning

src/​EFCore/​Infrastructure/​ModelValidator.cs:1439

TryGetSequenceType() also recognizes IAsyncEnumerable<T>, but PrimitiveCollection() only resolves IEnumerable<T> (see InternalTypeBaseBuilder.PrimitiveCollection). An explicitly scalar IAsyncEnumerable<T> property can therefore trigger this warning even though the suggested PrimitiveCollection() configuration throws NotCollection; restrict this check to the sequence shape that PrimitiveCollection() actually supports (or add equivalent async-collection support).

1 file not reviewed
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file

Review effort: Lite

Copilot AI lite review requested due to automatic review settings November 16, 2026 18:34

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.

🟢 Approval recommended

No active issues are identified, and the change is supported by targeted model-validation, SQL Server, configuration, localization, diagnostics, and API-baseline updates.

No open issues 🎉

1 resolved since last review
1 file not reviewed
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file

Review effort: Lite

@AndriySvyryd
AndriySvyryd merged commit f92a51b into release/11.0 Nov 16, 2026
19 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/fix-translation-error-collection-property branch November 16, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhelpful translation error when a collection property is configured with Property() in 11

6 participants