@@ -14,6 +14,21 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
14
14
public class SqlServerOptionsExtension : RelationalOptionsExtension
15
15
{
16
16
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 ;
17
32
18
33
/// <summary>
19
34
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -36,6 +51,7 @@ public SqlServerOptionsExtension()
36
51
protected SqlServerOptionsExtension ( SqlServerOptionsExtension copyFrom )
37
52
: base ( copyFrom )
38
53
{
54
+ _compatibilityLevel = copyFrom . _compatibilityLevel ;
39
55
}
40
56
41
57
/// <summary>
@@ -56,6 +72,39 @@ public override DbContextOptionsExtensionInfo Info
56
72
protected override RelationalOptionsExtension Clone ( )
57
73
=> new SqlServerOptionsExtension ( this ) ;
58
74
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
+
59
108
/// <summary>
60
109
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
61
110
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -81,7 +130,8 @@ public override bool IsDatabaseProvider
81
130
=> true ;
82
131
83
132
public override bool ShouldUseSameServiceProvider ( DbContextOptionsExtensionInfo other )
84
- => other is ExtensionInfo ;
133
+ => other is ExtensionInfo otherInfo
134
+ && Extension . CompatibilityLevel == otherInfo . Extension . CompatibilityLevel ;
85
135
86
136
public override string LogFragment
87
137
{
@@ -93,6 +143,13 @@ public override string LogFragment
93
143
94
144
builder . Append ( base . LogFragment ) ;
95
145
146
+ if ( Extension . _compatibilityLevel is int compatibilityLevel )
147
+ {
148
+ builder
149
+ . Append ( "CompatibilityLevel=" )
150
+ . Append ( compatibilityLevel ) ;
151
+ }
152
+
96
153
_logFragment = builder . ToString ( ) ;
97
154
}
98
155
@@ -101,6 +158,13 @@ public override string LogFragment
101
158
}
102
159
103
160
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
+ }
105
169
}
106
170
}
0 commit comments