Skip to content

Commit

Permalink
Add IServiceCollection.UseSqlServer<DbContext> extension methods
Browse files Browse the repository at this point in the history
Contributes to #25192
  • Loading branch information
DamianEdwards committed Jul 8, 2021
1 parent fa4115b commit cfb2314
Showing 1 changed file with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -30,6 +31,81 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public static class SqlServerServiceCollectionExtensions
{
/// <summary>
/// <para>
/// Registers the given Entity Framework context as a service in the <see cref="IServiceCollection" />
/// and configures it to connect to a SQL Server database.
/// </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 SQL Server 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 use the appropriate
/// <see cref="EntityFrameworkServiceCollectionExtensions.AddDbContext{TContext}(IServiceCollection, Action{DbContextOptionsBuilder}?, ServiceLifetime, ServiceLifetime)"/>
/// method and supply an optional action to configure the <see cref="DbContextOptions" /> for the context.
/// </para>
/// <para>
/// For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs.
/// For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.
/// </para>
/// </summary>
/// <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>
/// <returns> The same service collection so that multiple calls can be chained. </returns>
public static IServiceCollection AddSqlServer<TContext>(this IServiceCollection serviceCollection, string connectionString, Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null)
where TContext : DbContext
{
Check.NotNull(serviceCollection, nameof(serviceCollection));
Check.NotEmpty(connectionString, nameof(connectionString));

return serviceCollection.AddDbContext<TContext>(options => options.UseSqlServer(connectionString, sqlServerOptionsAction));
}

/// <summary>
/// <para>
/// Registers the given Entity Framework context as a service in the <see cref="IServiceCollection" />
/// and configures it to connect to a SQL Server database.
/// </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 SQL Server provider and connection string.
/// </para>
/// <para>
/// The connection or connection string must be set before the <see cref="DbContext" /> is used to connect
/// to a database. Set a connection using <see cref="RelationalDatabaseFacadeExtensions.SetDbConnection" />.
/// Set a connection string using <see cref="RelationalDatabaseFacadeExtensions.SetConnectionString" />.
/// </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 use the appropriate
/// <see cref="EntityFrameworkServiceCollectionExtensions.AddDbContext{TContext}(IServiceCollection, Action{DbContextOptionsBuilder}?, ServiceLifetime, ServiceLifetime)"/>
/// method and supply an optional action to configure the <see cref="DbContextOptions" /> for the context.
/// </para>
/// <para>
/// For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs.
/// For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.
/// </para>
/// </summary>
/// <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="sqlServerOptionsAction"> An optional action to allow additional SQL Server specific configuration. </param>
/// <returns> The same service collection so that multiple calls can be chained. </returns>
public static IServiceCollection AddSqlite<TContext>(this IServiceCollection serviceCollection, Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null)
where TContext : DbContext
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

return serviceCollection.AddDbContext<TContext>(options => options.UseSqlServer(sqlServerOptionsAction));
}

/// <summary>
/// <para>
/// Adds the services required by the Microsoft SQL Server database provider for Entity Framework
Expand Down

0 comments on commit cfb2314

Please sign in to comment.