Repository navigation
Fix OPENJSON AS JSON failure on Azure SQL when column type is json at compat level 170 - #38665
Merged
AndriySvyryd merged 5 commits intoJul 20, 2026
Merged
Conversation
…mpat level 170 (#38615) Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix exception when querying json on Azure SQL with compatibility level 170
Fix OPENJSON Jul 17, 2026
AS JSON failure on Azure SQL when column type is json at compat level 170
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts SQL Server JSON query SQL generation to avoid an Azure SQL limitation at compatibility level 170: OPENJSON ... WITH (...) columns using AS JSON cannot use the native json type on Azure SQL, so EF Core now emits nvarchar(max) for that specific case unless the server is on-prem SQL Server 2025+.
Changes:
- Introduces
SupportsJsonTypeInOpenJsononISqlServerSingletonOptions/SqlServerSingletonOptionsto distinguish on-prem SQL Server support from Azure SQL. - Updates
SqlServerQuerySqlGeneratorto substitutenvarchar(max)forjson-typedOPENJSON WITHcolumns whenAS JSONis used and the server doesn’t supportjsonin that position. - Adds a regression test covering nested primitive collections within complex collections inside a JSON column, with baselines for both
nvarchar(max)andjsoncolumn types.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/AdHocJsonQuerySqlServerTestBase.cs | Adds SQL assertions for the new regression scenario across JSON column type variants. |
| test/EFCore.Relational.Specification.Tests/Query/AdHocJsonQueryRelationalTestBase.cs | Adds provider-agnostic regression test scenario/model and verifies results. |
| src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs | Emits nvarchar(max) instead of json for OPENJSON ... WITH columns using AS JSON where required (Azure SQL). |
| src/EFCore.SqlServer/Infrastructure/Internal/SqlServerSingletonOptions.cs | Implements the new capability flag based on engine type/compatibility level. |
| src/EFCore.SqlServer/Infrastructure/Internal/ISqlServerSingletonOptions.cs | Adds the new capability flag to the singleton options contract. |
…erateColumnInfo Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ow assertion for nested JSON SelectMany test Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
cincuranet
approved these changes
Jul 20, 2026
This was referenced Jul 31, 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 #38615
Description
At compatibility level 170, EF Core generates JSON collection queries using syntax unsupported by Azure SQL, causing SQL error 13618. The fix uses nvarchar(max) for nested JSON fragments while preserving SQL Server 2025 behavior.
Customer impact
Queries over nested JSON collections fail instead of returning results, breaking affected application workflows. Workaround: configure EF’s Azure SQL provider with UseCompatibilityLevel(160), but this can result it degraded performance.
How found
Reported on EF Core 10.0.9 by 3 customers.
Regression
No. Native JSON and JSON-mapped complex collections were introduced in EF Core 10.
Testing
Test added
Risk
Medium. The product change is small and localized, but changing intermediate JSON fragments to text affects downstream operations.