Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce UseAzureSynapse and UseAzureSql #34052

Merged
merged 7 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// SQL Server specific extension methods for <see cref="IServiceCollection" />.
/// SQL Server, Azure SQL, Azure Synapse specific extension methods for <see cref="IServiceCollection" />.
/// </summary>
public static class SqlServerServiceCollectionExtensions
{
Expand Down Expand Up @@ -45,14 +45,14 @@ public static class SqlServerServiceCollectionExtensions
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server, Azure SQL, Azure Synapse databases with EF Core</see>
/// for more information and examples.
/// </para>
/// </remarks>
/// <typeparam name="TContext">The type of context to be registered.</typeparam>
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="connectionString">The connection string of the database to connect to.</param>
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server specific configuration.</param>
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server, Azure SQL, Azure Synapse specific configuration.</param>
/// <param name="optionsAction">An optional action to configure the <see cref="DbContextOptions" /> for the context.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddSqlServer<TContext>(
Expand Down Expand Up @@ -91,6 +91,157 @@ public static IServiceCollection AddSqlServer<TContext>(
/// </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceCollection AddEntityFrameworkSqlServer(this IServiceCollection serviceCollection)
=> AddEntityFrameworkSqlEngine(serviceCollection);

/// <summary>
/// Registers the given Entity Framework <see cref="DbContext" /> as a service in the <see cref="IServiceCollection" />
/// and configures it to connect to a Azure SQL database.
/// </summary>
/// <remarks>
/// <para>
/// This method is a shortcut for configuring a <see cref="DbContext" /> to use Azure SQL. It does not support all options.
/// Use <see cref="O:EntityFrameworkServiceCollectionExtensions.AddDbContext" /> and related methods for full control of
/// this process.
/// </para>
/// <para>
/// Use this method when using dependency injection in your application, such as with ASP.NET Core.
/// For applications that don't use dependency injection, consider creating <see cref="DbContext" />
/// instances directly with its constructor. The <see cref="DbContext.OnConfiguring" /> method can then be
/// overridden to configure the Azure SQL provider and connection string.
/// </para>
/// <para>
/// To configure the <see cref="DbContextOptions{TContext}" /> for the context, either override the
/// <see cref="DbContext.OnConfiguring" /> method in your derived context, or supply
/// an optional action to configure the <see cref="DbContextOptions" /> for the context.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-di">Using DbContext with dependency injection</see> for more information and examples.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server, Azure SQL, Azure Synapse databases with EF Core</see>
/// for more information and examples.
/// </para>
/// </remarks>
/// <typeparam name="TContext">The type of context to be registered.</typeparam>
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="connectionString">The connection string of the database to connect to.</param>
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server, Azure SQL, Azure Synapse specific configuration.</param>
/// <param name="optionsAction">An optional action to configure the <see cref="DbContextOptions" /> for the context.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddAzureSql<TContext>(
this IServiceCollection serviceCollection,
string? connectionString,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null,
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
=> serviceCollection.AddDbContext<TContext>(
(_, options) =>
{
optionsAction?.Invoke(options);
options.UseAzureSql(connectionString, sqlServerOptionsAction);
});

/// <summary>
/// <para>
/// Adds the services required by the Microsoft Azure SQL database provider for Entity Framework
/// to an <see cref="IServiceCollection" />.
/// </para>
/// <para>
/// Warning: Do not call this method accidentally. It is much more likely you need
/// to call <see cref="AddAzureSql{TContext}" />.
/// </para>
/// </summary>
/// <remarks>
/// Calling this method is no longer necessary when building most applications, including those that
/// use dependency injection in ASP.NET or elsewhere.
/// It is only needed when building the internal service provider for use with
/// the <see cref="DbContextOptionsBuilder.UseInternalServiceProvider" /> method.
/// This is not recommend other than for some advanced scenarios.
/// </remarks>
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>
/// The same service collection so that multiple calls can be chained.
/// </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceCollection AddEntityFrameworkAzureSql(this IServiceCollection serviceCollection)
=> AddEntityFrameworkSqlEngine(serviceCollection);

/// <summary>
/// Registers the given Entity Framework <see cref="DbContext" /> as a service in the <see cref="IServiceCollection" />
/// and configures it to connect to a Azure Synapse database.
/// </summary>
/// <remarks>
/// <para>
/// This method is a shortcut for configuring a <see cref="DbContext" /> to use Azure Synapse. It does not support all options.
/// Use <see cref="O:EntityFrameworkServiceCollectionExtensions.AddDbContext" /> and related methods for full control of
/// this process.
/// </para>
/// <para>
/// Use this method when using dependency injection in your application, such as with ASP.NET Core.
/// For applications that don't use dependency injection, consider creating <see cref="DbContext" />
/// instances directly with its constructor. The <see cref="DbContext.OnConfiguring" /> method can then be
/// overridden to configure the Azure Synapse provider and connection string.
/// </para>
/// <para>
/// To configure the <see cref="DbContextOptions{TContext}" /> for the context, either override the
/// <see cref="DbContext.OnConfiguring" /> method in your derived context, or supply
/// an optional action to configure the <see cref="DbContextOptions" /> for the context.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-di">Using DbContext with dependency injection</see> for more information and examples.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server, Azure SQL, Azure Synapse databases with EF Core</see>
/// for more information and examples.
/// </para>
/// </remarks>
/// <typeparam name="TContext">The type of context to be registered.</typeparam>
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="connectionString">The connection string of the database to connect to.</param>
/// <param name="sqlServerOptionsAction">An optional action to allow additional SQL Server, Azure SQL, Azure Synapse specific configuration.</param>
/// <param name="optionsAction">An optional action to configure the <see cref="DbContextOptions" /> for the context.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddAzureSynapse<TContext>(
this IServiceCollection serviceCollection,
string? connectionString,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null,
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
=> serviceCollection.AddDbContext<TContext>(
(_, options) =>
{
optionsAction?.Invoke(options);
options.UseAzureSynapse(connectionString, sqlServerOptionsAction);
});

/// <summary>
/// <para>
/// Adds the services required by the Microsoft Azure Synapse database provider for Entity Framework
/// to an <see cref="IServiceCollection" />.
/// </para>
/// <para>
/// Warning: Do not call this method accidentally. It is much more likely you need
/// to call <see cref="AddAzureSynapse{TContext}" />.
/// </para>
/// </summary>
/// <remarks>
/// Calling this method is no longer necessary when building most applications, including those that
/// use dependency injection in ASP.NET or elsewhere.
/// It is only needed when building the internal service provider for use with
/// the <see cref="DbContextOptionsBuilder.UseInternalServiceProvider" /> method.
/// This is not recommend other than for some advanced scenarios.
/// </remarks>
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>
/// The same service collection so that multiple calls can be chained.
/// </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceCollection AddEntityFrameworkAzureSynapse(this IServiceCollection serviceCollection)
=> AddEntityFrameworkSqlEngine(serviceCollection);

private static IServiceCollection AddEntityFrameworkSqlEngine(IServiceCollection serviceCollection)
{
new EntityFrameworkRelationalServicesBuilder(serviceCollection)
.TryAdd<LoggingDefinitions, SqlServerLoggingDefinitions>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@ public interface ISqlServerSingletonOptions : ISingletonOptions
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
int CompatibilityLevel { get; }
public SqlServerEngineType EngineType { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
int? CompatibilityLevelWithoutDefault { get; }
public int SqlServerCompatibilityLevel { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public int AzureSqlCompatibilityLevel { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public int AzureSynapseCompatibilityLevel { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public enum SqlServerEngineType
{
/// <summary>
/// Unknown SQL engine type.
/// </summary>
Unknown = 0,

/// <summary>
/// SQL Server.
/// </summary>
SqlServer = 1,

/// <summary>
/// Azure SQL.
/// </summary>
AzureSql = 2,

/// <summary>
/// Azure Synapse.
/// </summary>
AzureSynapse = 3,
}
Loading