Skip to content

Conversation

@roji
Copy link
Member

@roji roji commented Jul 26, 2025

  • Remove experimental warning
  • Remove conversion to nvarchar(max) within OPENJSON
  • Add conversions to nvarchar(max) when comparing json (not natively supported) - discuss in design
  • Use json type in complex JSON query tests when targeting newer versions of SQL Server
  • Various test tweaks and cleanup

Closes #36024

/cc @artl93

@roji roji force-pushed the SqlServerJson branch 3 times, most recently from 50b7c3d to 8812300 Compare July 26, 2025 10:38
// When testing against SQL Server 2025 or later, set the compatibility level to 170 to use the json type instead of nvarchar(max).
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> UsingJsonType
? builder.UseSqlServer(o => o.UseCompatibilityLevel(170))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the two different testing strategies:

  • These relationship query tests use the same suites to test both new and old SQL Server versions (with different translations), varying the compatibility level based on the actual server being targeted. This means that for full coverage, our CI must run both the newest and the oldest supported SQL Server version.
    • Baselines can assert on the different translations (see just below). This is really nice for seeing the differences.
    • Less duplication, just one test suite covers both translations (assuming we run against the different versions in CI).
  • The alternative is done in PrimitiveCollectionsQuerySqlServerJsonTypeTest, where there's a separate test suite only for the new translations, which gets skipped if the targeted SQL Server doesn't support the feature.
    • This causes lots of duplication and is less manageable.

typeof(string),
typeMappingSource.FindMapping(typeof(string))!))
: base.Visit(expression);
// The SQL Server json type cannot be compared ("The JSON data type cannot be compared or sorted, except when using the
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to confirm this in design.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design decision: we'll go ahead with allowing comparison of JSON-mapped complex types by adding this cast to nvarchar(max):

  • This isn't any worse than the already-supported comparison of nvarchar(max)-mapped types.
  • As long as EF is used to read/write the columns, everything works.
  • We don't want the new json support to be inferior to the worst nvarchar(max) mapping option.

jsonScalar.IsNullable);
}

case SqlServerOpenJsonExpression openJsonExpression:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that this cast is no longer needed - OPENJSON can just directly accept the json data type.

@roji roji marked this pull request as ready for review July 26, 2025 14:52
@roji roji requested a review from a team as a code owner July 26, 2025 14:52
@roji roji enabled auto-merge (squash) July 29, 2025 19:55
@roji
Copy link
Member Author

roji commented Jul 29, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@roji
Copy link
Member Author

roji commented Jul 30, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@roji
Copy link
Member Author

roji commented Jul 30, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

* Remove experimental warning
* Remove conversion to nvarchar(max) within OPENJSON
* Add conversions to nvarchar(max) when comparing json
  (not natively supported)
* Use json type in complex JSON query tests when targeting newer
  versions of SQL Server
* Various test tweaks and cleanup

Closes dotnet#36024
@roji roji merged commit 50fc45d into dotnet:main Jul 30, 2025
7 checks passed
@roji roji deleted the SqlServerJson branch July 30, 2025 21:47
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.

SQL Server: bring JSON type out of experimental status

2 participants