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
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 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
On Azure SQL with compatibility level 170, EF Core generates
[col] json '$.path' AS JSONin OPENJSON's WITH clause for nativejson-typed columns. Azure SQL rejects this with error 13618 — only SQL Server 2025 on-premises supports thejsondata type in that position; Azure SQL requiresnvarchar(max).Changes
SqlServerQuerySqlGenerator.GenerateColumnInfo: when emitting a WITH-clause column that usesAS JSONand hasStoreType == "json", substitutesnvarchar(max)unlessSupportsJsonTypeInOpenJsonis true. This leaves SQL Server 2025 behavior unchanged while fixing Azure SQL.Regression test
SelectMany_over_primitive_collection_nested_in_complex_collection_inside_json_columnadded toAdHocJsonQueryRelationalTestBase/AdHocJsonQuerySqlServerTestBase, covering bothnvarchar(max)andjsoncolumn type variants.