|
15 | 15 | namespace Microsoft.Extensions.DependencyInjection;
|
16 | 16 |
|
17 | 17 | /// <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" />. |
19 | 19 | /// </summary>
|
20 | 20 | public static class SqlServerServiceCollectionExtensions
|
21 | 21 | {
|
@@ -45,14 +45,14 @@ public static class SqlServerServiceCollectionExtensions
|
45 | 45 | /// </para>
|
46 | 46 | /// <para>
|
47 | 47 | /// 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> |
49 | 49 | /// for more information and examples.
|
50 | 50 | /// </para>
|
51 | 51 | /// </remarks>
|
52 | 52 | /// <typeparam name="TContext">The type of context to be registered.</typeparam>
|
53 | 53 | /// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
|
54 | 54 | /// <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> |
56 | 56 | /// <param name="optionsAction">An optional action to configure the <see cref="DbContextOptions" /> for the context.</param>
|
57 | 57 | /// <returns>The same service collection so that multiple calls can be chained.</returns>
|
58 | 58 | public static IServiceCollection AddSqlServer<TContext>(
|
@@ -91,6 +91,157 @@ public static IServiceCollection AddSqlServer<TContext>(
|
91 | 91 | /// </returns>
|
92 | 92 | [EditorBrowsable(EditorBrowsableState.Never)]
|
93 | 93 | 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) |
94 | 245 | {
|
95 | 246 | new EntityFrameworkRelationalServicesBuilder(serviceCollection)
|
96 | 247 | .TryAdd<LoggingDefinitions, SqlServerLoggingDefinitions>()
|
|
0 commit comments