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
servicesIServiceCollectionThe service collection.
configurationIConfigurationThe configuration containing connection strings.
connectionNamestringThe name of the connection string in the configuration.
sqlServerOptionsActionAction<SqlServerDbContextOptionsBuilder>Optional action to configure SQL Server-specific options.
contextLifetimeServiceLifetimeThe lifetime of the DbContext in the DI container. Defaults to Scoped.
Returns
- IServiceCollection
The service collection for chaining.
Type Parameters
TContextThe 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
servicesIServiceCollectionThe service collection.
connectionNamestringThe name of the connection string in the settings file.
settingsFileNamestringThe settings file name. Defaults to "appsettings.json".
sqlServerOptionsActionAction<SqlServerDbContextOptionsBuilder>Optional action to configure SQL Server-specific options.
contextLifetimeServiceLifetimeThe lifetime of the DbContext in the DI container. Defaults to Scoped.
Returns
- IServiceCollection
The service collection for chaining.
Type Parameters
TContextThe 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
servicesIServiceCollectionThe service collection.
connectionStringstringThe SQL Server connection string.
sqlServerOptionsActionAction<SqlServerDbContextOptionsBuilder>Optional action to configure SQL Server-specific options.
contextLifetimeServiceLifetimeThe lifetime of the DbContext in the DI container. Defaults to Scoped.
Returns
- IServiceCollection
The service collection for chaining.
Type Parameters
TContextThe type of DbContext to register.