Skip to content

[release/11.0] Fix SQL Server JSON_CONTAINS type handling - #39806

Merged
cincuranet merged 1 commit into
release/11.0from
cincuranet-backport-json-contains-fix
Dec 2, 2026
Merged

cincuranet merged 1 commit into
release/11.0from
cincuranet-backport-json-contains-fix

Conversation

@cincuranet

@cincuranet cincuranet commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39625
Backports #39706

Description

EF Core 11 translates primitive collection Contains on SQL Server/Azure SQL (compatibility level 170, native json columns) to JSON_CONTAINS without checking the search value's type, but JSON_CONTAINS accepts only numeric, character and bit values. GUID and temporal (DateTime, DateTimeOffset, TimeOnly) searches fail with SQL error 8116, and value-converted elements can compare mismatched representations. The fix uses JSON_CONTAINS only for unconverted elements with a matching supported store type and otherwise falls back to the existing OPENJSON translation.

Customer impact

Queries such as context.Blogs.Where(b => b.Ids.Contains(id)) over Guid/temporal primitive collections stored as json throw on SQL Server 2025 and Azure SQL, including collections in ToJson owned types. Workarounds: rewrite as .Where(x => x == id).Any() or lower the compatibility level below 170.

How found

Customer reported on 11.0.0 (SQL Server 2025 and Azure SQL).

Regression

No. The JSON_CONTAINS translation is new in 11.0 (#37562); this is a bug in new 11.0 functionality.

Testing

Added a SQL-translation unit test (SQL Server and Azure SQL, compatibility levels 160 and 170) and a functional sync/async test for SQL Server (normal, json type, old) and SQLite. Existing primitive collection tests pass. The quirk was verified via a negative control.

Risk

Low. The change only narrows when JSON_CONTAINS is used; affected cases fall back to the long-established OPENJSON path. Quirk: Microsoft.EntityFrameworkCore.Issue39625.

- Use JSON_CONTAINS only for unconverted elements with a matching supported numeric, character or bit store type
- Fall back to OPENJSON otherwise
- Add Microsoft.EntityFrameworkCore.Issue39625 quirk

Fixes #39625
@cincuranet
cincuranet requested review from a team and wtgodbe as code owners November 25, 2026 10:32
Copilot AI balanced review requested due to automatic review settings November 25, 2026 10:32

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 review issues are recorded for the SQL Server collection-query translation backport.

No open issues 🎉

What changed in this PR

Backports #39706 to EF Core 11’s release branch, fixing #39625 by avoiding SQL Server JSON_CONTAINS for unsupported or value-converted collection elements.

Changes:

  • Restricts JSON_CONTAINS to unconverted elements with matching supported store types; otherwise uses OPENJSON.
  • Adds the Microsoft.EntityFrameworkCore.Issue39625 switch to restore the previous behavior.
  • Adds translation tests, sync/async regression coverage, provider SQL baselines, and query-pipeline guidance.
File Description
test/​EFCore.SqlServer.Tests/​Query/​SqlServerJsonContainsTest.cs Tests SQL Server and Azure SQL translation at compatibility levels 160 and 170.
test/​EFCore.SqlServer.FunctionalTests/​Query/​PrimitiveCollectionsQuerySqlServerTest.cs Adds SQL Server fallback SQL baselines.
test/​EFCore.SqlServer.FunctionalTests/​Query/​PrimitiveCollectionsQuerySqlServerJsonTypeTest.cs Adds fallback baselines for native JSON columns.
test/​EFCore.SqlServer.FunctionalTests/​Query/​PrimitiveCollectionsQueryOldSqlServerTest.cs Verifies the expected low-compatibility failure.
test/​EFCore.Sqlite.FunctionalTests/​Query/​PrimitiveCollectionsQuerySqliteTest.cs Adds SQLite baselines for the shared regression test.
test/​EFCore.Relational.Specification.Tests/​Query/​PrimitiveCollectionsQueryRelationalTestBase.cs Adds shared GUID, temporal, owned-JSON, and converted-element coverage.
src/​EFCore.SqlServer/​Query/​Internal/​SqlServerQueryableMethodTranslatingExpressionVisitor.cs Guards JSON_CONTAINS by element mapping and search type, with an opt-out switch.
.agents/​skills/​query-pipeline/​SKILL.md Documents search-type restrictions and the fallback requirement.

Review effort: Balanced

@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage

release/11.0 94d3895 cincuranet-backport-... b73927e +/-
77% 68% -9%
View coverage of the 10 most impacted files.
File release/11.0 94d3895 cincuranet-backport-... b73927e +/-
/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.

@cincuranet
cincuranet requested a review from artl93 November 25, 2026 11:47

@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.

New feature adoption. Approved.

@cincuranet
cincuranet merged commit cb4c1d0 into release/11.0 Dec 2, 2026
21 checks passed
@cincuranet
cincuranet deleted the cincuranet-backport-json-contains-fix branch December 2, 2026 07:51
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.

4 participants