Skip to content

Fix OPENJSON AS JSON failure on Azure SQL when column type is json at compat level 170 - #38665

Merged
AndriySvyryd merged 5 commits into
release/10.0from
copilot/fix-json-query-exception-azure-sql
Jul 20, 2026
Merged

AndriySvyryd merged 5 commits into
release/10.0from
copilot/fix-json-query-exception-azure-sql

Conversation

Copilot AI commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

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.

…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 AS JSON failure on Azure SQL when column type is json at compat level 170 Jul 17, 2026
Copilot AI requested a review from AndriySvyryd July 17, 2026 04:56
@AndriySvyryd
AndriySvyryd requested a review from Copilot July 17, 2026 19:48

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.

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 SupportsJsonTypeInOpenJson on ISqlServerSingletonOptions/SqlServerSingletonOptions to distinguish on-prem SQL Server support from Azure SQL.
  • Updates SqlServerQuerySqlGenerator to substitute nvarchar(max) for json-typed OPENJSON WITH columns when AS JSON is used and the server doesn’t support json in that position.
  • Adds a regression test covering nested primitive collections within complex collections inside a JSON column, with baselines for both nvarchar(max) and json column 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.

Comment thread src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs Outdated
…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>
Comment thread src/EFCore.SqlServer/Infrastructure/Internal/ISqlServerSingletonOptions.cs Outdated
…ow assertion for nested JSON SelectMany test

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested a review from AndriySvyryd July 18, 2026 00:11
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review July 18, 2026 01:14
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner July 18, 2026 01:14
Copilot AI review requested due to automatic review settings July 18, 2026 01:14
@AndriySvyryd
AndriySvyryd requested a review from cincuranet July 18, 2026 01:16

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs
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.

Exeption when querying json, that happens only on Azure SQL with compatibility level 170

5 participants