Skip to content

Commit 16ea450

Browse files
authored
Introduce UseAzureSynapse and UseAzureSql (#34052)
1 parent 1e32b1d commit 16ea450

25 files changed

+1431
-151
lines changed

src/EFCore.SqlServer/Extensions/SqlServerDbContextOptionsBuilderExtensions.cs

+542-35
Large diffs are not rendered by default.

src/EFCore.SqlServer/Extensions/SqlServerServiceCollectionExtensions.cs

+154-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Microsoft.Extensions.DependencyInjection;
1616

1717
/// <summary>
18-
/// SQL Server specific extension methods for <see cref="IServiceCollection" />.
18+
/// SQL Server, Azure SQL, Azure Synapse specific extension methods for <see cref="IServiceCollection" />.
1919
/// </summary>
2020
public static class SqlServerServiceCollectionExtensions
2121
{
@@ -45,14 +45,14 @@ public static class SqlServerServiceCollectionExtensions
4545
/// </para>
4646
/// <para>
4747
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
48-
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
48+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server, Azure SQL, Azure Synapse databases with EF Core</see>
4949
/// for more information and examples.
5050
/// </para>
5151
/// </remarks>
5252
/// <typeparam name="TContext">The type of context to be registered.</typeparam>
5353
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
5454
/// <param name="connectionString">The connection string of the database to connect to.</param>
55-
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server specific configuration.</param>
55+
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server, Azure SQL, Azure Synapse specific configuration.</param>
5656
/// <param name="optionsAction">An optional action to configure the <see cref="DbContextOptions" /> for the context.</param>
5757
/// <returns>The same service collection so that multiple calls can be chained.</returns>
5858
public static IServiceCollection AddSqlServer<TContext>(
@@ -91,6 +91,157 @@ public static IServiceCollection AddSqlServer<TContext>(
9191
/// </returns>
9292
[EditorBrowsable(EditorBrowsableState.Never)]
9393
public static IServiceCollection AddEntityFrameworkSqlServer(this IServiceCollection serviceCollection)
94+
=> AddEntityFrameworkSqlEngine(serviceCollection);
95+
96+
/// <summary>
97+
/// Registers the given Entity Framework <see cref="DbContext" /> as a service in the <see cref="IServiceCollection" />
98+
/// and configures it to connect to a Azure SQL database.
99+
/// </summary>
100+
/// <remarks>
101+
/// <para>
102+
/// This method is a shortcut for configuring a <see cref="DbContext" /> to use Azure SQL. It does not support all options.
103+
/// Use <see cref="O:EntityFrameworkServiceCollectionExtensions.AddDbContext" /> and related methods for full control of
104+
/// this process.
105+
/// </para>
106+
/// <para>
107+
/// Use this method when using dependency injection in your application, such as with ASP.NET Core.
108+
/// For applications that don't use dependency injection, consider creating <see cref="DbContext" />
109+
/// instances directly with its constructor. The <see cref="DbContext.OnConfiguring" /> method can then be
110+
/// overridden to configure the Azure SQL provider and connection string.
111+
/// </para>
112+
/// <para>
113+
/// To configure the <see cref="DbContextOptions{TContext}" /> for the context, either override the
114+
/// <see cref="DbContext.OnConfiguring" /> method in your derived context, or supply
115+
/// an optional action to configure the <see cref="DbContextOptions" /> for the context.
116+
/// </para>
117+
/// <para>
118+
/// See <see href="https://aka.ms/efcore-docs-di">Using DbContext with dependency injection</see> for more information and examples.
119+
/// </para>
120+
/// <para>
121+
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
122+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server, Azure SQL, Azure Synapse databases with EF Core</see>
123+
/// for more information and examples.
124+
/// </para>
125+
/// </remarks>
126+
/// <typeparam name="TContext">The type of context to be registered.</typeparam>
127+
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
128+
/// <param name="connectionString">The connection string of the database to connect to.</param>
129+
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server, Azure SQL, Azure Synapse specific configuration.</param>
130+
/// <param name="optionsAction">An optional action to configure the <see cref="DbContextOptions" /> for the context.</param>
131+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
132+
public static IServiceCollection AddAzureSql<TContext>(
133+
this IServiceCollection serviceCollection,
134+
string? connectionString,
135+
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null,
136+
Action<DbContextOptionsBuilder>? optionsAction = null)
137+
where TContext : DbContext
138+
=> serviceCollection.AddDbContext<TContext>(
139+
(_, options) =>
140+
{
141+
optionsAction?.Invoke(options);
142+
options.UseAzureSql(connectionString, sqlServerOptionsAction);
143+
});
144+
145+
/// <summary>
146+
/// <para>
147+
/// Adds the services required by the Microsoft Azure SQL database provider for Entity Framework
148+
/// to an <see cref="IServiceCollection" />.
149+
/// </para>
150+
/// <para>
151+
/// Warning: Do not call this method accidentally. It is much more likely you need
152+
/// to call <see cref="AddAzureSql{TContext}" />.
153+
/// </para>
154+
/// </summary>
155+
/// <remarks>
156+
/// Calling this method is no longer necessary when building most applications, including those that
157+
/// use dependency injection in ASP.NET or elsewhere.
158+
/// It is only needed when building the internal service provider for use with
159+
/// the <see cref="DbContextOptionsBuilder.UseInternalServiceProvider" /> method.
160+
/// This is not recommend other than for some advanced scenarios.
161+
/// </remarks>
162+
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
163+
/// <returns>
164+
/// The same service collection so that multiple calls can be chained.
165+
/// </returns>
166+
[EditorBrowsable(EditorBrowsableState.Never)]
167+
public static IServiceCollection AddEntityFrameworkAzureSql(this IServiceCollection serviceCollection)
168+
=> AddEntityFrameworkSqlEngine(serviceCollection);
169+
170+
/// <summary>
171+
/// Registers the given Entity Framework <see cref="DbContext" /> as a service in the <see cref="IServiceCollection" />
172+
/// and configures it to connect to a Azure Synapse database.
173+
/// </summary>
174+
/// <remarks>
175+
/// <para>
176+
/// This method is a shortcut for configuring a <see cref="DbContext" /> to use Azure Synapse. It does not support all options.
177+
/// Use <see cref="O:EntityFrameworkServiceCollectionExtensions.AddDbContext" /> and related methods for full control of
178+
/// this process.
179+
/// </para>
180+
/// <para>
181+
/// Use this method when using dependency injection in your application, such as with ASP.NET Core.
182+
/// For applications that don't use dependency injection, consider creating <see cref="DbContext" />
183+
/// instances directly with its constructor. The <see cref="DbContext.OnConfiguring" /> method can then be
184+
/// overridden to configure the Azure Synapse provider and connection string.
185+
/// </para>
186+
/// <para>
187+
/// To configure the <see cref="DbContextOptions{TContext}" /> for the context, either override the
188+
/// <see cref="DbContext.OnConfiguring" /> method in your derived context, or supply
189+
/// an optional action to configure the <see cref="DbContextOptions" /> for the context.
190+
/// </para>
191+
/// <para>
192+
/// See <see href="https://aka.ms/efcore-docs-di">Using DbContext with dependency injection</see> for more information and examples.
193+
/// </para>
194+
/// <para>
195+
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
196+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server, Azure SQL, Azure Synapse databases with EF Core</see>
197+
/// for more information and examples.
198+
/// </para>
199+
/// </remarks>
200+
/// <typeparam name="TContext">The type of context to be registered.</typeparam>
201+
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
202+
/// <param name="connectionString">The connection string of the database to connect to.</param>
203+
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server, Azure SQL, Azure Synapse specific configuration.</param>
204+
/// <param name="optionsAction">An optional action to configure the <see cref="DbContextOptions" /> for the context.</param>
205+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
206+
public static IServiceCollection AddAzureSynapse<TContext>(
207+
this IServiceCollection serviceCollection,
208+
string? connectionString,
209+
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null,
210+
Action<DbContextOptionsBuilder>? optionsAction = null)
211+
where TContext : DbContext
212+
=> serviceCollection.AddDbContext<TContext>(
213+
(_, options) =>
214+
{
215+
optionsAction?.Invoke(options);
216+
options.UseAzureSynapse(connectionString, sqlServerOptionsAction);
217+
});
218+
219+
/// <summary>
220+
/// <para>
221+
/// Adds the services required by the Microsoft Azure Synapse database provider for Entity Framework
222+
/// to an <see cref="IServiceCollection" />.
223+
/// </para>
224+
/// <para>
225+
/// Warning: Do not call this method accidentally. It is much more likely you need
226+
/// to call <see cref="AddAzureSynapse{TContext}" />.
227+
/// </para>
228+
/// </summary>
229+
/// <remarks>
230+
/// Calling this method is no longer necessary when building most applications, including those that
231+
/// use dependency injection in ASP.NET or elsewhere.
232+
/// It is only needed when building the internal service provider for use with
233+
/// the <see cref="DbContextOptionsBuilder.UseInternalServiceProvider" /> method.
234+
/// This is not recommend other than for some advanced scenarios.
235+
/// </remarks>
236+
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
237+
/// <returns>
238+
/// The same service collection so that multiple calls can be chained.
239+
/// </returns>
240+
[EditorBrowsable(EditorBrowsableState.Never)]
241+
public static IServiceCollection AddEntityFrameworkAzureSynapse(this IServiceCollection serviceCollection)
242+
=> AddEntityFrameworkSqlEngine(serviceCollection);
243+
244+
private static IServiceCollection AddEntityFrameworkSqlEngine(IServiceCollection serviceCollection)
94245
{
95246
new EntityFrameworkRelationalServicesBuilder(serviceCollection)
96247
.TryAdd<LoggingDefinitions, SqlServerLoggingDefinitions>()

src/EFCore.SqlServer/Infrastructure/Internal/ISqlServerSingletonOptions.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,29 @@ public interface ISqlServerSingletonOptions : ISingletonOptions
1717
/// any release. You should only use it directly in your code with extreme caution and knowing that
1818
/// doing so can result in application failures when updating to a new Entity Framework Core release.
1919
/// </summary>
20-
int CompatibilityLevel { get; }
20+
public SqlServerEngineType EngineType { get; }
2121

2222
/// <summary>
2323
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
2424
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
2525
/// any release. You should only use it directly in your code with extreme caution and knowing that
2626
/// doing so can result in application failures when updating to a new Entity Framework Core release.
2727
/// </summary>
28-
int? CompatibilityLevelWithoutDefault { get; }
28+
public int SqlServerCompatibilityLevel { get; }
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 int AzureSqlCompatibilityLevel { get; }
37+
38+
/// <summary>
39+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
40+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
41+
/// any release. You should only use it directly in your code with extreme caution and knowing that
42+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
43+
/// </summary>
44+
public int AzureSynapseCompatibilityLevel { get; }
2945
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 enum SqlServerEngineType
13+
{
14+
/// <summary>
15+
/// Unknown SQL engine type.
16+
/// </summary>
17+
Unknown = 0,
18+
19+
/// <summary>
20+
/// SQL Server.
21+
/// </summary>
22+
SqlServer = 1,
23+
24+
/// <summary>
25+
/// Azure SQL.
26+
/// </summary>
27+
AzureSql = 2,
28+
29+
/// <summary>
30+
/// Azure Synapse.
31+
/// </summary>
32+
AzureSynapse = 3,
33+
}

0 commit comments

Comments
 (0)