Skip to content

Commit ac9790b

Browse files
committed
Additions to SQL Server JSON breaking changes (#5093)
1 parent 8923440 commit ac9790b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

entity-framework/core/what-is-new/ef-core-10.0/breaking-changes.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ CREATE TABLE [Blogs] (
7676
);
7777
```
7878

79+
Although the new JSON data type is the recommended way to store JSON data in SQL Server going forward, there may be some behavioral differences when transitioning from `nvarchar(max)`, and some specific querying forms may not be supported. For example, SQL Server does not support the DISTINCT operator over JSON arrays, and queries attempting to do so will fail.
80+
7981
Note that if you have an existing table and are using <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseAzureSql*>, upgrading to EF 10 will cause a migration to be generated which alters all existing `nvarchar(max)` JSON columns to `json`. This alter operation is supported and should get applied seamlessly and without any issues, but is a non-trivial change to your database.
8082

8183
> [!NOTE]
@@ -87,16 +89,18 @@ The new JSON data type introduced by SQL Server is a superior, 1st-class way to
8789

8890
#### Mitigations
8991

90-
If you do not wish to transition to the new JSON data type right away, you can configure EF with a compatibility level lower than 170:
92+
If you are targeting Azure SQL Database and do not wish to transition to the new JSON data type right away, you can configure EF with a compatibility level lower than 170:
9193

9294
```c#
9395
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
9496
{
95-
optionsBuilder.UseSqlServer("<connection string>", o => o.UseCompatibilityLevel(160));
97+
optionsBuilder.UseAzureSql("<connection string>", o => o.UseCompatibilityLevel(160));
9698
}
9799
```
98100

99-
As an alternative, you can explicitly set the column type for your properties to be `nvarchar(max)`:
101+
If you're targeting on-premises SQL Server, the default compatibility level with `UseSqlServer` is currently 150 (SQL Server 2019), so the JSON data type is not used.
102+
103+
As an alternative, you can explicitly set the column type on specific properties to be `nvarchar(max)`:
100104

101105
```c#
102106
public class Blog
@@ -109,6 +113,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
109113
{
110114
modelBuilder.Entity<Blog>().PrimitiveCollection(b => b.Tags).HasColumnType("nvarchar(max)");
111115
modelBuilder.Entity<Blog>().OwnsMany(b => b.Posts, b => b.ToJson().HasColumnType("nvarchar(max)"));
116+
modelBuilder.Entity<Blog>().ComplexProperty(e => e.Posts, b => b.ToJson());
112117
}
113118
```
114119

0 commit comments

Comments
 (0)