Skip to content

Commit f6f9e47

Browse files
committed
Implement SQL Server compatibility level
Closes dotnet#30163
1 parent d4599b1 commit f6f9e47

File tree

5 files changed

+177
-2
lines changed

5 files changed

+177
-2
lines changed

Diff for: src/EFCore.SqlServer/Extensions/SqlServerServiceCollectionExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ public static IServiceCollection AddEntityFrameworkSqlServer(this IServiceCollec
127127
.TryAdd<INavigationExpansionExtensibilityHelper, SqlServerNavigationExpansionExtensibilityHelper>()
128128
.TryAdd<IQueryableMethodTranslatingExpressionVisitorFactory, SqlServerQueryableMethodTranslatingExpressionVisitorFactory>()
129129
.TryAdd<IExceptionDetector, SqlServerExceptionDetector>()
130+
.TryAdd<ISingletonOptions, ISqlServerSingletonOptions>(p => p.GetRequiredService<ISqlServerSingletonOptions>())
130131
.TryAddProviderSpecificServices(
131132
b => b
133+
.TryAddSingleton<ISqlServerSingletonOptions, SqlServerSingletonOptions>()
132134
.TryAddSingleton<ISqlServerValueGeneratorCache, SqlServerValueGeneratorCache>()
133135
.TryAddSingleton<ISqlServerUpdateSqlGenerator, SqlServerUpdateSqlGenerator>()
134136
.TryAddSingleton<ISqlServerSequenceValueGeneratorFactory, SqlServerSequenceValueGeneratorFactory>()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
5+
6+
/// <summary>
7+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
8+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
9+
/// any release. You should only use it directly in your code with extreme caution and knowing that
10+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
11+
/// </summary>
12+
public interface ISqlServerSingletonOptions : ISingletonOptions
13+
{
14+
/// <summary>
15+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
16+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
17+
/// any release. You should only use it directly in your code with extreme caution and knowing that
18+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
19+
/// </summary>
20+
int CompatibilityLevel { get; }
21+
22+
/// <summary>
23+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
24+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
25+
/// any release. You should only use it directly in your code with extreme caution and knowing that
26+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
27+
/// </summary>
28+
int? CompatibilityLevelWithoutDefault { get; }
29+
}

Diff for: src/EFCore.SqlServer/Infrastructure/Internal/SqlServerOptionsExtension.cs

+66-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
1414
public class SqlServerOptionsExtension : RelationalOptionsExtension
1515
{
1616
private DbContextOptionsExtensionInfo? _info;
17+
private int? _compatibilityLevel;
18+
19+
/// <summary>
20+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
21+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
22+
/// any release. You should only use it directly in your code with extreme caution and knowing that
23+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
24+
/// </summary>
25+
// See https://learn.microsoft.com/sql/t-sql/statements/alter-database-transact-sql-compatibility-level
26+
// SQL Server 2022 (16.x): compatibility level 160, start date 2022-11-16, mainstream end date 2028-01-11, extended end date 2033-01-11
27+
// SQL Server 2019 (15.x): compatibility level 150, start date 2019-11-04, mainstream end date 2025-02-28, extended end date 2030-01-08
28+
// SQL Server 2017 (14.x): compatibility level 140, start date 2017-09-29, mainstream end date 2022-10-11, extended end date 2027-10-12
29+
// SQL Server 2016 (13.x): compatibility level 130, start date 2016-06-01, mainstream end date 2021-07-13, extended end date 2026-07-14
30+
// SQL Server 2014 (12.x): compatibility level 120, start date 2014-06-05, mainstream end date 2019-07-09, extended end date 2024-07-09
31+
private static readonly int DefaultCompatibilityLevel = 160;
1732

1833
/// <summary>
1934
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -36,6 +51,7 @@ public SqlServerOptionsExtension()
3651
protected SqlServerOptionsExtension(SqlServerOptionsExtension copyFrom)
3752
: base(copyFrom)
3853
{
54+
_compatibilityLevel = copyFrom._compatibilityLevel;
3955
}
4056

4157
/// <summary>
@@ -56,6 +72,39 @@ public override DbContextOptionsExtensionInfo Info
5672
protected override RelationalOptionsExtension Clone()
5773
=> new SqlServerOptionsExtension(this);
5874

75+
/// <summary>
76+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
77+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
78+
/// any release. You should only use it directly in your code with extreme caution and knowing that
79+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
80+
/// </summary>
81+
public virtual int CompatibilityLevel
82+
=> _compatibilityLevel ?? DefaultCompatibilityLevel;
83+
84+
/// <summary>
85+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
86+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
87+
/// any release. You should only use it directly in your code with extreme caution and knowing that
88+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
89+
/// </summary>
90+
public virtual int? CompatibilityLevelWithoutDefault
91+
=> _compatibilityLevel;
92+
93+
/// <summary>
94+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
95+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
96+
/// any release. You should only use it directly in your code with extreme caution and knowing that
97+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
98+
/// </summary>
99+
public virtual SqlServerOptionsExtension WithCompatibilityLevel(int? compatibilityLevel)
100+
{
101+
var clone = (SqlServerOptionsExtension)Clone();
102+
103+
clone._compatibilityLevel = compatibilityLevel;
104+
105+
return clone;
106+
}
107+
59108
/// <summary>
60109
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
61110
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -81,7 +130,8 @@ public override bool IsDatabaseProvider
81130
=> true;
82131

83132
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other)
84-
=> other is ExtensionInfo;
133+
=> other is ExtensionInfo otherInfo
134+
&& Extension.CompatibilityLevel == otherInfo.Extension.CompatibilityLevel;
85135

86136
public override string LogFragment
87137
{
@@ -93,6 +143,13 @@ public override string LogFragment
93143

94144
builder.Append(base.LogFragment);
95145

146+
if (Extension._compatibilityLevel is int compatibilityLevel)
147+
{
148+
builder
149+
.Append("CompatibilityLevel=")
150+
.Append(compatibilityLevel);
151+
}
152+
96153
_logFragment = builder.ToString();
97154
}
98155

@@ -101,6 +158,13 @@ public override string LogFragment
101158
}
102159

103160
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
104-
=> debugInfo["SqlServer"] = "1";
161+
{
162+
debugInfo["SqlServer"] = "1";
163+
164+
if (Extension.CompatibilityLevel is int compatibilityLevel)
165+
{
166+
debugInfo["CompatibilityLevel"] = compatibilityLevel.ToString();
167+
}
168+
}
105169
}
106170
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
5+
6+
/// <summary>
7+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
8+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
9+
/// any release. You should only use it directly in your code with extreme caution and knowing that
10+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
11+
/// </summary>
12+
public class SqlServerSingletonOptions : ISqlServerSingletonOptions
13+
{
14+
/// <summary>
15+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
16+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
17+
/// any release. You should only use it directly in your code with extreme caution and knowing that
18+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
19+
/// </summary>
20+
public virtual int CompatibilityLevel { get; private set; }
21+
22+
/// <summary>
23+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
24+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
25+
/// any release. You should only use it directly in your code with extreme caution and knowing that
26+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
27+
/// </summary>
28+
public virtual int? CompatibilityLevelWithoutDefault { get; private set; }
29+
30+
/// <summary>
31+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
32+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
33+
/// any release. You should only use it directly in your code with extreme caution and knowing that
34+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
35+
/// </summary>
36+
public virtual void Initialize(IDbContextOptions options)
37+
{
38+
var sqlServerOptions = options.FindExtension<SqlServerOptionsExtension>();
39+
if (sqlServerOptions != null)
40+
{
41+
CompatibilityLevel = sqlServerOptions.CompatibilityLevel;
42+
CompatibilityLevelWithoutDefault = sqlServerOptions.CompatibilityLevelWithoutDefault;
43+
}
44+
}
45+
46+
/// <summary>
47+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
48+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
49+
/// any release. You should only use it directly in your code with extreme caution and knowing that
50+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
51+
/// </summary>
52+
public virtual void Validate(IDbContextOptions options)
53+
{
54+
var sqlserverOptions = options.FindExtension<SqlServerOptionsExtension>();
55+
56+
if (sqlserverOptions != null &&
57+
(CompatibilityLevelWithoutDefault != sqlserverOptions.CompatibilityLevelWithoutDefault
58+
|| CompatibilityLevel != sqlserverOptions.CompatibilityLevel))
59+
{
60+
throw new InvalidOperationException(
61+
CoreStrings.SingletonOptionChanged(
62+
nameof(SqlServerDbContextOptionsExtensions.UseSqlServer),
63+
nameof(DbContextOptionsBuilder.UseInternalServiceProvider)));
64+
}
65+
}
66+
}

Diff for: src/EFCore.SqlServer/Infrastructure/SqlServerDbContextOptionsBuilder.cs

+14
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,18 @@ public virtual SqlServerDbContextOptionsBuilder EnableRetryOnFailure(
103103
TimeSpan maxRetryDelay,
104104
IEnumerable<int>? errorNumbersToAdd)
105105
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c, maxRetryCount, maxRetryDelay, errorNumbersToAdd));
106+
107+
/// <summary>
108+
/// Sets the SQL Server compatibility level that EF Core will use when interacting with the database. This allows configuring EF
109+
/// Core to work with older (or newer) versions of SQL Server. Defaults to <c>150</c> (SQL Server 2019).
110+
/// </summary>
111+
/// <remarks>
112+
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
113+
/// <see href="https://learn.microsoft.com/sql/t-sql/statements/alter-database-transact-sql-compatibility-level">SQL Server
114+
/// documentation on compatibility level</see> for more information and examples.
115+
/// </remarks>
116+
/// <param name="compatibilityLevel"><see langword="false" /> to have null resource</param>
117+
// TODO: Naming; Cosmos doesn't have Use/Set, so just CompatibilityLevel? SetCompatibilityLevel?
118+
public virtual SqlServerDbContextOptionsBuilder UseCompatibilityLevel(int compatibilityLevel)
119+
=> WithOption(e => e.WithCompatibilityLevel(compatibilityLevel));
106120
}

0 commit comments

Comments
 (0)