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

Fix OPENJSON AS JSON failure on Azure SQL when column type is json at compat level 170#38665
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

On Azure SQL with compatibility level 170, EF Core generates [col] json '$.path' AS JSON in OPENJSON's WITH clause for native json-typed columns. Azure SQL rejects this with error 13618 — only SQL Server 2025 on-premises supports the json data type in that position; Azure SQL requires nvarchar(max).

Changes

  • SqlServerQuerySqlGenerator.GenerateColumnInfo: when emitting a WITH-clause column that uses AS JSON and has StoreType == "json", substitutes nvarchar(max) unless SupportsJsonTypeInOpenJson is true. This leaves SQL Server 2025 behavior unchanged while fixing Azure SQL.

    -- Before (fails on Azure SQL with error 13618):
    CROSS APPLY OPENJSON([c].[CarConfiguration], '$.optionPackages') WITH (
        [partNumbers] json '$.partNumbers' AS JSON   -- ← rejected by Azure SQL
    )
    
    -- After (Azure SQL / nvarchar(max) column type):
    CROSS APPLY OPENJSON([c].[CarConfiguration], '$.optionPackages') WITH (
        [partNumbers] nvarchar(max) '$.partNumbers' AS JSON   -- ← valid everywhere
    )
  • Regression test SelectMany_over_primitive_collection_nested_in_complex_collection_inside_json_column added to AdHocJsonQueryRelationalTestBase / AdHocJsonQuerySqlServerTestBase, covering both nvarchar(max) and json column type variants.

…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

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

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
@AndriySvyryd
AndriySvyryd merged commit 23c978a into release/10.0 Jul 20, 2026
10 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/fix-json-query-exception-azure-sql branch July 20, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

4 participants