Skip to content
DocsTry Aspire
DocsTry

AspireAzureEFPostgreSqlExtensions Methods

ClassMethods2 members
Provides extension methods for registering a PostgreSQL database context in an Aspire application.
AddAzureNpgsqlDbContext(IHostApplicationBuilder, string, Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>, Action<DbContextOptionsBuilder>)Section titled AddAzureNpgsqlDbContext(IHostApplicationBuilder, string, Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>, Action<DbContextOptionsBuilder>)extension
Registers the given EntityFrameworkCore.DbContext as a service in the services provided by the builder. Enables db context pooling, retries, corresponding health check, logging and telemetry.
public static class AspireAzureEFPostgreSqlExtensions
{
public static void AddAzureNpgsqlDbContext<TContext>(
this IHostApplicationBuilder builder,
string connectionName,
Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>? configureSettings = null,
Action<DbContextOptionsBuilder>? configureDbContextOptions = null)
{
// ...
}
}
builderIHostApplicationBuilderThe Hosting.IHostApplicationBuilder to read config from and add services to.
connectionNamestringA name used to retrieve the connection string from the ConnectionStrings configuration section.
configureSettingsAction<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>optionalAn optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.
configureDbContextOptionsAction<DbContextOptionsBuilder>optionalAn optional delegate to configure the EntityFrameworkCore.DbContextOptions for the context.
ArgumentNullExceptionThrown if mandatory builder is null.
InvalidOperationExceptionThrown when mandatory NpgsqlEntityFrameworkCorePostgreSQLSettings.ConnectionString is not provided.

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

The DbContext.OnConfiguring method can then be overridden to configure EntityFrameworkCore.DbContext options.

EnrichAzureNpgsqlDbContext(IHostApplicationBuilder, Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>)Section titled EnrichAzureNpgsqlDbContext(IHostApplicationBuilder, Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>)extension
Configures retries, health check, logging and telemetry for the EntityFrameworkCore.DbContext.
public static class AspireAzureEFPostgreSqlExtensions
{
public static void EnrichAzureNpgsqlDbContext<TContext>(
this IHostApplicationBuilder builder,
Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>? configureSettings = null)
{
// ...
}
}
builderIHostApplicationBuilder
configureSettingsAction<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>optional
ArgumentNullExceptionThrown if mandatory builder is null.
InvalidOperationExceptionThrown when mandatory EntityFrameworkCore.DbContext is not registered in DI.