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

Add IServiceCollection.Use[DbProviderName]<DbContext> extension methods #25220

Merged
merged 8 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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.
JunTaoLuo marked this conversation as resolved.
Show resolved Hide resolved
/// </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)
JunTaoLuo marked this conversation as resolved.
Show resolved Hide resolved
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)
JunTaoLuo marked this conversation as resolved.
Show resolved Hide resolved
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
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 @@ -29,6 +30,81 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public static class SqliteServiceCollectionExtensions
{
/// <summary>
/// <para>
/// Registers the given Entity Framework context as a service in the <see cref="IServiceCollection" />
/// and configures it to connect to a SQLite 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 SQLite 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.
JunTaoLuo marked this conversation as resolved.
Show resolved Hide resolved
/// </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="sqliteOptionsAction"> An optional action to allow additional SQLite specific configuration. </param>
/// <returns> The same service collection so that multiple calls can be chained. </returns>
public static IServiceCollection AddSqlite<TContext>(this IServiceCollection serviceCollection, string connectionString, Action<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null)
where TContext : DbContext
{
Check.NotNull(serviceCollection, nameof(serviceCollection));
Check.NotEmpty(connectionString, nameof(connectionString));

return serviceCollection.AddDbContext<TContext>(options => options.UseSqlite(connectionString, sqliteOptionsAction));
}

/// <summary>
/// <para>
/// Registers the given Entity Framework context as a service in the <see cref="IServiceCollection" />
/// and configures it to connect to a SQLite 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 SQLite 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="sqliteOptionsAction"> An optional action to allow additional SQLite 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<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null)
JunTaoLuo marked this conversation as resolved.
Show resolved Hide resolved
JunTaoLuo marked this conversation as resolved.
Show resolved Hide resolved
where TContext : DbContext
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

return serviceCollection.AddDbContext<TContext>(options => options.UseSqlite(sqliteOptionsAction));
}

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