Skip to content

Commit 58fc08f

Browse files
Shay RojanskyCopilot
andcommitted
Document SQL Server compatibility level bump
Document dotnet/efcore#38198 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c0d05f0 commit 58fc08f

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This page documents API and behavior changes that have the potential to break ex
2424
|:--------------------------------------------------------------------------------------------------------------- | -----------|
2525
| [EF tools now require framework to be specified for multi-targeted projects](#ef-tools-multi-targeting) | Medium |
2626
| [Application Name is now injected into the connection string](#sqlserver-application-name) | Low |
27+
| [SQL Server compatibility level now defaults to 160](#sqlserver-compatibility-level-160) | Low |
2728
| [SQL Server json data type used by default on Azure SQL and compatibility level 170](#sqlserver-json-data-type) | Low |
2829
| [Parameterized collections now use multiple parameters by default](#parameterized-collections) | Low |
2930
| [ExecuteUpdateAsync now accepts a regular, non-expression lambda](#ExecuteUpdateAsync-lambda) | Low |
@@ -96,6 +97,39 @@ When a connection string without an `Application Name` is passed to EF, EF now i
9697

9798
A mitigation is to simply define an `Application Name` in your connection string. Once one is defined, EF does not overwrite it and the original connection string is preserved exactly as-is.
9899

100+
<a name="sqlserver-compatibility-level-160"></a>
101+
102+
### SQL Server compatibility level now defaults to 160
103+
104+
[Tracking Issue #38198](https://github.com/dotnet/efcore/issues/38198)
105+
106+
#### Old behavior
107+
108+
Previously, when using <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer*> without explicitly configuring a SQL Server compatibility level, EF Core defaulted to compatibility level 150, corresponding to SQL Server 2019.
109+
110+
#### New behavior
111+
112+
Starting with EF Core 10.0, <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer*> defaults to compatibility level 160, corresponding to SQL Server 2022. This allows EF to generate SQL which uses SQL Server 2022 features by default. For example, some queries now use `LEAST` and `GREATEST`, including translations for `Math.Min`, `Math.Max`, <xref:Microsoft.EntityFrameworkCore.RelationalDbFunctionsExtensions.Least*>, <xref:Microsoft.EntityFrameworkCore.RelationalDbFunctionsExtensions.Greatest*>, and some `Take`/`Skip` patterns.
113+
114+
If your database runs on SQL Server 2019 or older, or is configured with a compatibility level lower than 160, some SQL generated by EF Core may no longer be supported by the database.
115+
116+
#### Why
117+
118+
SQL Server 2022 has been available for several years, and using compatibility level 160 by default allows EF Core to generate simpler and more efficient SQL for newer SQL Server versions.
119+
120+
#### Mitigations
121+
122+
If your database does not support compatibility level 160, configure EF Core to use the compatibility level supported by your database:
123+
124+
```c#
125+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
126+
{
127+
optionsBuilder.UseSqlServer("<connection string>", o => o.UseCompatibilityLevel(150));
128+
}
129+
```
130+
131+
For more information, see the [SQL Server compatibility level documentation](xref:core/providers/sql-server/index#compatibility-level).
132+
99133
<a name="sqlserver-json-data-type"></a>
100134

101135
### SQL Server json data type used by default on Azure SQL and compatibility level 170
@@ -164,7 +198,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
164198
}
165199
```
166200

167-
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.
201+
If you're targeting on-premises SQL Server, the default compatibility level with `UseSqlServer` is currently 160 (SQL Server 2022), so the JSON data type is not used.
168202

169203
As an alternative, you can explicitly set the column type on specific properties to be `nvarchar(max)`:
170204

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ WHERE JSON_VALUE([b].[Details], '$.Viewers' RETURNING int) > 3
122122

123123
Note that if your EF application already uses JSON via `nvarchar` columns, these columns will be automatically changed to `json` with the first migration. You can opt out of this by manually setting the column type to `nvarchar(max)`, or configuring a compatibility level lower than 170.
124124

125+
<a name="sql-server-compatibility-level-160"></a>
126+
127+
### Default compatibility level changed to SQL Server 2022
128+
129+
When using <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer*> without explicitly configuring a SQL Server compatibility level, EF Core now defaults to compatibility level 160 (SQL Server 2022), rather than 150 (SQL Server 2019). This enables SQL Server 2022-specific SQL translations by default.
130+
131+
For example, `Math.Min`, `Math.Max`, <xref:Microsoft.EntityFrameworkCore.RelationalDbFunctionsExtensions.Least*>, <xref:Microsoft.EntityFrameworkCore.RelationalDbFunctionsExtensions.Greatest*>, and `Min`/`Max` over inline or primitive collections can now translate to `LEAST` and `GREATEST` by default. EF can also use `LEAST` to simplify some queries with multiple `Take` operators, or with `Skip` and `Take`:
132+
133+
```sql
134+
SELECT [c].[CustomerID], [c].[ContactName]
135+
FROM [Customers] AS [c]
136+
ORDER BY [c].[ContactTitle], [c].[ContactName]
137+
OFFSET @p ROWS FETCH NEXT LEAST(@p1, @p2) ROWS ONLY
138+
```
139+
140+
If your database is SQL Server 2019 or older, or has a compatibility level lower than 160, configure EF's SQL Server compatibility level explicitly. For more information, see the [breaking change note](xref:core/what-is-new/ef-core-10.0/breaking-changes#sqlserver-compatibility-level-160) and the [SQL Server provider documentation](xref:core/providers/sql-server/index#compatibility-level).
141+
125142
<a name="default-constraint-names"></a>
126143

127144
### Custom default constraint names

0 commit comments

Comments
 (0)