Table of Contents

Class ServiceCollectionExtensions

Namespace
NuvTools.Data.EntityFrameworkCore.SqlServer.Extensions
Assembly
NuvTools.Data.EntityFrameworkCore.SqlServer.dll

Extension methods for registering SQL Server DbContext instances in the dependency injection container.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions
Inherited Members

Methods

AddDatabaseByConnectionName<TContext>(IServiceCollection, IConfiguration, string, Action<SqlServerDbContextOptionsBuilder>?, ServiceLifetime)

Registers a SQL Server DbContext in the service collection using a connection name from an existing configuration.

public static IServiceCollection AddDatabaseByConnectionName<TContext>(this IServiceCollection services, IConfiguration configuration, string connectionName, Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped) where TContext : DbContext

Parameters

services IServiceCollection

The service collection.

configuration IConfiguration

The configuration containing connection strings.

connectionName string

The name of the connection string in the configuration.

sqlServerOptionsAction Action<SqlServerDbContextOptionsBuilder>

Optional action to configure SQL Server-specific options.

contextLifetime ServiceLifetime

The lifetime of the DbContext in the DI container. Defaults to Scoped.

Returns

IServiceCollection

The service collection for chaining.

Type Parameters

TContext

The type of DbContext to register.

AddDatabaseByConnectionName<TContext>(IServiceCollection, string, string, Action<SqlServerDbContextOptionsBuilder>?, ServiceLifetime)

Registers a SQL Server DbContext in the service collection using a connection name from a settings file.

public static IServiceCollection AddDatabaseByConnectionName<TContext>(this IServiceCollection services, string connectionName, string settingsFileName = "appsettings.json", Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped) where TContext : DbContext

Parameters

services IServiceCollection

The service collection.

connectionName string

The name of the connection string in the settings file.

settingsFileName string

The settings file name. Defaults to "appsettings.json".

sqlServerOptionsAction Action<SqlServerDbContextOptionsBuilder>

Optional action to configure SQL Server-specific options.

contextLifetime ServiceLifetime

The lifetime of the DbContext in the DI container. Defaults to Scoped.

Returns

IServiceCollection

The service collection for chaining.

Type Parameters

TContext

The type of DbContext to register.

AddDatabase<TContext>(IServiceCollection, string?, Action<SqlServerDbContextOptionsBuilder>?, ServiceLifetime)

Registers a SQL Server DbContext in the service collection using a connection string.

public static IServiceCollection AddDatabase<TContext>(this IServiceCollection services, string? connectionString, Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped) where TContext : DbContext

Parameters

services IServiceCollection

The service collection.

connectionString string

The SQL Server connection string.

sqlServerOptionsAction Action<SqlServerDbContextOptionsBuilder>

Optional action to configure SQL Server-specific options.

contextLifetime ServiceLifetime

The lifetime of the DbContext in the DI container. Defaults to Scoped.

Returns

IServiceCollection

The service collection for chaining.

Type Parameters

TContext

The type of DbContext to register.