Class ServiceCollectionExtensions
- Namespace
- NuvTools.Data.EntityFrameworkCore.PostgreSQL.Extensions
- Assembly
- NuvTools.Data.EntityFrameworkCore.PostgreSQL.dll
Extension methods for registering PostgreSQL DbContext instances in the dependency injection container.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
- Inherited Members
Methods
AddDatabaseByConnectionName<TContext>(IServiceCollection, IConfiguration, string, Action<NpgsqlDbContextOptionsBuilder>?, ServiceLifetime)
Registers a PostgreSQL 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<NpgsqlDbContextOptionsBuilder>? npgsqlOptionsAction = 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.
npgsqlOptionsActionAction<NpgsqlDbContextOptionsBuilder>Optional action to configure PostgreSQL-specific options via Npgsql.
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<NpgsqlDbContextOptionsBuilder>?, ServiceLifetime)
Registers a PostgreSQL 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<NpgsqlDbContextOptionsBuilder>? npgsqlOptionsAction = 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".
npgsqlOptionsActionAction<NpgsqlDbContextOptionsBuilder>Optional action to configure PostgreSQL-specific options via Npgsql.
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<NpgsqlDbContextOptionsBuilder>?, ServiceLifetime)
Registers a PostgreSQL DbContext in the service collection using a connection string.
public static IServiceCollection AddDatabase<TContext>(this IServiceCollection services, string? connectionString, Action<NpgsqlDbContextOptionsBuilder>? npgsqlOptionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped) where TContext : DbContext
Parameters
servicesIServiceCollectionThe service collection.
connectionStringstringThe PostgreSQL connection string.
npgsqlOptionsActionAction<NpgsqlDbContextOptionsBuilder>Optional action to configure PostgreSQL-specific options via Npgsql.
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.