Share via


AspireEFPostgreSqlExtensions.AddNpgsqlDbContext<TContext> Method

Definition

Registers the given DbContext as a service in the services provided by the builder. Enables db context pooling, retries, corresponding health check, logging and telemetry.

public static void AddNpgsqlDbContext<TContext> (this Microsoft.Extensions.Hosting.IHostApplicationBuilder builder, string connectionName, Action<Aspire.Npgsql.EntityFrameworkCore.PostgreSQL.NpgsqlEntityFrameworkCorePostgreSQLSettings>? configureSettings = default, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? configureDbContextOptions = default) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddNpgsqlDbContext : Microsoft.Extensions.Hosting.IHostApplicationBuilder * string * Action<Aspire.Npgsql.EntityFrameworkCore.PostgreSQL.NpgsqlEntityFrameworkCorePostgreSQLSettings> * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> -> unit (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Sub AddNpgsqlDbContext(Of TContext As DbContext) (builder As IHostApplicationBuilder, connectionName As String, Optional configureSettings As Action(Of NpgsqlEntityFrameworkCorePostgreSQLSettings) = Nothing, Optional configureDbContextOptions As Action(Of DbContextOptionsBuilder) = Nothing)

Type Parameters

TContext

The DbContext that needs to be registered.

Parameters

builder
IHostApplicationBuilder

The IHostApplicationBuilder to read config from and add services to.

connectionName
String

A name used to retrieve the connection string from the ConnectionStrings configuration section.

configureSettings
Action<NpgsqlEntityFrameworkCorePostgreSQLSettings>

An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.

configureDbContextOptions
Action<DbContextOptionsBuilder>

An optional delegate to configure the DbContextOptions for the context.

Exceptions

Thrown if mandatory builder is null.

Thrown when mandatory ConnectionString is not provided.

Remarks

Reads the configuration from "Aspire:Npgsql:EntityFrameworkCore:PostgreSQL:{typeof(TContext).Name}" config section, or "Aspire:Npgsql:EntityFrameworkCore:PostgreSQL" if former does not exist.

The OnConfiguring(DbContextOptionsBuilder) method can then be overridden to configure DbContext options.

Applies to