Table of Contents

Class NpgsqlServiceCollectionExtensions

Namespace
Microsoft.Extensions.DependencyInjection
Assembly
Npgsql.EntityFrameworkCore.PostgreSQL.dll

Provides extension methods to configure Entity Framework Core for Npgsql.

public static class NpgsqlServiceCollectionExtensions
Inheritance
NpgsqlServiceCollectionExtensions
Inherited Members

Methods

AddEntityFrameworkNpgsql(IServiceCollection)

Adds the services required by the Npgsql database provider for Entity Framework to an IServiceCollection.

Calling this method is no longer necessary when building most applications, including those that use dependency injection in ASP.NET or elsewhere. It is only needed when building the internal service provider for use with the UseInternalServiceProvider(IServiceProvider) method. This is not recommend other than for some advanced scenarios.

public static IServiceCollection AddEntityFrameworkNpgsql(this IServiceCollection serviceCollection)

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

Returns

IServiceCollection

The same service collection so that multiple calls can be chained.

AddNpgsql<TContext>(IServiceCollection, string?, Action<NpgsqlDbContextOptionsBuilder>?, Action<DbContextOptionsBuilder>?)

Registers the given Entity Framework context as a service in the IServiceCollection and configures it to connect to a PostgreSQL database.

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 DbContext instances directly with its constructor. The OnConfiguring(DbContextOptionsBuilder) method can then be overridden to configure the SQL Server provider and connection string.

To configure the DbContextOptions<TContext> for the context, either override the OnConfiguring(DbContextOptionsBuilder) method in your derived context, or supply an optional action to configure the DbContextOptions for the context.

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.

public static IServiceCollection AddNpgsql<TContext>(this IServiceCollection serviceCollection, string? connectionString, Action<NpgsqlDbContextOptionsBuilder>? npgsqlOptionsAction = null, Action<DbContextOptionsBuilder>? optionsAction = null) where TContext : DbContext

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

connectionString string

The connection string of the database to connect to.

npgsqlOptionsAction Action<NpgsqlDbContextOptionsBuilder>

An optional action to allow additional SQL Server specific configuration.

optionsAction Action<DbContextOptionsBuilder>

An optional action to configure the DbContextOptions for the context.

Returns

IServiceCollection

The same service collection so that multiple calls can be chained.

Type Parameters

TContext

The type of context to be registered.