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

Split UseSqlServer/UseAzureSql/UseAzureSynapse builders #34385

Merged
merged 3 commits into from
Aug 9, 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 @@ -52,7 +52,7 @@ public static class SqlServerServiceCollectionExtensions
/// <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="sqlServerOptionsAction">An optional action to allow additional SQL Server 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 @@ -126,20 +126,20 @@ public static IServiceCollection AddEntityFrameworkSqlServer(this IServiceCollec
/// <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="azureSqlOptionsAction">An optional action to allow additional Azure SQL 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<AzureSqlDbContextOptionsBuilder>? azureSqlOptionsAction = null,
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
=> serviceCollection.AddDbContext<TContext>(
(_, options) =>
{
optionsAction?.Invoke(options);
options.UseAzureSql(connectionString, sqlServerOptionsAction);
options.UseAzureSql(connectionString, azureSqlOptionsAction);
});

/// <summary>
Expand Down Expand Up @@ -200,20 +200,20 @@ public static IServiceCollection AddEntityFrameworkAzureSql(this IServiceCollect
/// <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="azureSynapseOptionsAction">An optional action to allow additional 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<AzureSynapseDbContextOptionsBuilder>? azureSynapseOptionsAction = null,
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
=> serviceCollection.AddDbContext<TContext>(
(_, options) =>
{
optionsAction?.Invoke(options);
options.UseAzureSynapse(connectionString, sqlServerOptionsAction);
options.UseAzureSynapse(connectionString, azureSynapseOptionsAction);
});

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;

namespace Microsoft.EntityFrameworkCore.Infrastructure;

/// <summary>
/// Allows Azure SQL specific configuration to be performed on <see cref="DbContextOptions" />.
/// </summary>
/// <remarks>
/// Instances of this class are returned from a call to
/// <see cref="O:SqlServerDbContextOptionsExtensions.UseAzureSql" />
/// and it is not designed to be directly constructed in your application code.
/// </remarks>
public class AzureSqlDbContextOptionsBuilder
: RelationalDbContextOptionsBuilder<AzureSqlDbContextOptionsBuilder, SqlServerOptionsExtension>
{
/// <summary>
/// Initializes a new instance of the <see cref="AzureSqlDbContextOptionsBuilder" /> class.
/// </summary>
/// <param name="optionsBuilder">The options builder.</param>
public AzureSqlDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
: base(optionsBuilder)
{
}

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure SQL. It is pre-configured with
/// error numbers for transient errors that can be retried.
/// </para>
/// <para>
/// Default values of 6 for the maximum retry count and 30 seconds for the maximum default delay are used.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
public virtual AzureSqlDbContextOptionsBuilder EnableRetryOnFailure()
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c));

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure SQL. It is pre-configured with
/// error numbers for transient errors that can be retried.
/// </para>
/// <para>
/// A default value 30 seconds for the maximum default delay is used.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
public virtual AzureSqlDbContextOptionsBuilder EnableRetryOnFailure(int maxRetryCount)
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c, maxRetryCount));

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure SQL. It is pre-configured with
/// error numbers for transient errors that can be retried.
/// </para>
/// <para>
/// Default values of 6 for the maximum retry count and 30 seconds for the maximum default delay are used.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
/// <param name="errorNumbersToAdd">Additional SQL error numbers that should be considered transient.</param>
public virtual AzureSqlDbContextOptionsBuilder EnableRetryOnFailure(ICollection<int> errorNumbersToAdd)
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c, errorNumbersToAdd));

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure SQL. It is pre-configured with
/// error numbers for transient errors that can be retried, but additional error numbers can also be supplied.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
/// <param name="maxRetryCount">The maximum number of retry attempts.</param>
/// <param name="maxRetryDelay">The maximum delay between retries.</param>
/// <param name="errorNumbersToAdd">Additional SQL error numbers that should be considered transient.</param>
public virtual AzureSqlDbContextOptionsBuilder EnableRetryOnFailure(
int maxRetryCount,
TimeSpan maxRetryDelay,
IEnumerable<int>? errorNumbersToAdd)
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c, maxRetryCount, maxRetryDelay, errorNumbersToAdd));

/// <summary>
/// Sets the Azure SQL compatibility level that EF Core will use when interacting with the database. This allows configuring EF
/// Core to work with older (or newer) versions of Azure SQL. Defaults to <c>160</c>.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
/// <see href="https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-scoped-configuration-transact-sql">
/// Azure SQL documentation on compatibility level
/// </see>
/// for more information and examples.
/// </remarks>
/// <param name="compatibilityLevel"><see langword="false" /> to have null resource</param>
public virtual AzureSqlDbContextOptionsBuilder UseCompatibilityLevel(int compatibilityLevel)
=> WithOption(e => e.WithAzureSqlCompatibilityLevel(compatibilityLevel));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;

namespace Microsoft.EntityFrameworkCore.Infrastructure;

/// <summary>
/// Allows Azure Synapse specific configuration to be performed on <see cref="DbContextOptions" />.
/// </summary>
/// <remarks>
/// Instances of this class are returned from a call to
/// <see cref="O:SqlServerDbContextOptionsExtensions.UseAzureSynapse" />
/// and it is not designed to be directly constructed in your application code.
/// </remarks>
public class AzureSynapseDbContextOptionsBuilder
: RelationalDbContextOptionsBuilder<AzureSynapseDbContextOptionsBuilder, SqlServerOptionsExtension>
{
/// <summary>
/// Initializes a new instance of the <see cref="AzureSynapseDbContextOptionsBuilder" /> class.
/// </summary>
/// <param name="optionsBuilder">The options builder.</param>
public AzureSynapseDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
: base(optionsBuilder)
{
}

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure Synapse. It is pre-configured with
/// error numbers for transient errors that can be retried.
/// </para>
/// <para>
/// Default values of 6 for the maximum retry count and 30 seconds for the maximum default delay are used.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
public virtual AzureSynapseDbContextOptionsBuilder EnableRetryOnFailure()
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c));

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure Synapse. It is pre-configured with
/// error numbers for transient errors that can be retried.
/// </para>
/// <para>
/// A default value 30 seconds for the maximum default delay is used.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
public virtual AzureSynapseDbContextOptionsBuilder EnableRetryOnFailure(int maxRetryCount)
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c, maxRetryCount));

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure Synapse. It is pre-configured with
/// error numbers for transient errors that can be retried.
/// </para>
/// <para>
/// Default values of 6 for the maximum retry count and 30 seconds for the maximum default delay are used.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
/// <param name="errorNumbersToAdd">Additional SQL error numbers that should be considered transient.</param>
public virtual AzureSynapseDbContextOptionsBuilder EnableRetryOnFailure(ICollection<int> errorNumbersToAdd)
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c, errorNumbersToAdd));

/// <summary>
/// Configures the context to use the default retrying <see cref="IExecutionStrategy" />.
/// </summary>
/// <remarks>
/// <para>
/// This strategy is specifically tailored to Azure Synapse. It is pre-configured with
/// error numbers for transient errors that can be retried, but additional error numbers can also be supplied.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
/// <param name="maxRetryCount">The maximum number of retry attempts.</param>
/// <param name="maxRetryDelay">The maximum delay between retries.</param>
/// <param name="errorNumbersToAdd">Additional SQL error numbers that should be considered transient.</param>
public virtual AzureSynapseDbContextOptionsBuilder EnableRetryOnFailure(
int maxRetryCount,
TimeSpan maxRetryDelay,
IEnumerable<int>? errorNumbersToAdd)
=> ExecutionStrategy(c => new SqlServerRetryingExecutionStrategy(c, maxRetryCount, maxRetryDelay, errorNumbersToAdd));

/// <summary>
/// Sets the Azure Synapse compatibility level that EF Core will use when interacting with the database. This allows configuring EF
/// Core to work with older (or newer) versions of Azure Synapse. Defaults to <c>30</c>.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-dbcontext-options">Using DbContextOptions</see>, and
/// <see href="https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-scoped-configuration-transact-sql">
/// Azure Synapse documentation on compatibility level
/// </see>
/// for more information and examples.
/// </remarks>
/// <param name="compatibilityLevel"><see langword="false" /> to have null resource</param>
public virtual AzureSynapseDbContextOptionsBuilder UseCompatibilityLevel(int compatibilityLevel)
=> WithOption(e => e.WithAzureSynapseCompatibilityLevel(compatibilityLevel));
}
Loading