Repository navigation
[release/11.0] Fix SQL Server JSON_CONTAINS type handling - #39806
Merged
Merged
Conversation
- 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
Contributor
There was a problem hiding this comment.
🟢 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_CONTAINSto unconverted elements with matching supported store types; otherwise usesOPENJSON. - Adds the
Microsoft.EntityFrameworkCore.Issue39625switch 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
Code coverage overviewLanguages: C# C# / code-coverage
View coverage of the 10 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. |
artl93
approved these changes
Nov 30, 2026
artl93
left a comment
Member
There was a problem hiding this comment.
New feature adoption. Approved.
AndriySvyryd
approved these changes
Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #39625
Backports #39706
Description
EF Core 11 translates primitive collection
Containson SQL Server/Azure SQL (compatibility level 170, nativejsoncolumns) toJSON_CONTAINSwithout checking the search value's type, butJSON_CONTAINSaccepts 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 usesJSON_CONTAINSonly for unconverted elements with a matching supported store type and otherwise falls back to the existingOPENJSONtranslation.Customer impact
Queries such as
context.Blogs.Where(b => b.Ids.Contains(id))overGuid/temporal primitive collections stored asjsonthrow on SQL Server 2025 and Azure SQL, including collections inToJsonowned 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_CONTAINStranslation 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_CONTAINSis used; affected cases fall back to the long-establishedOPENJSONpath. Quirk:Microsoft.EntityFrameworkCore.Issue39625.