Table of Contents

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

services IServiceCollection

The service collection.

configuration IConfiguration

The configuration containing connection strings.

connectionName string

The name of the connection string in the configuration.

npgsqlOptionsAction Action<NpgsqlDbContextOptionsBuilder>

Optional action to configure PostgreSQL-specific options via Npgsql.

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<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

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".

npgsqlOptionsAction Action<NpgsqlDbContextOptionsBuilder>

Optional action to configure PostgreSQL-specific options via Npgsql.

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<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

services IServiceCollection

The service collection.

connectionString string

The PostgreSQL connection string.

npgsqlOptionsAction Action<NpgsqlDbContextOptionsBuilder>

Optional action to configure PostgreSQL-specific options via Npgsql.

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.