Adliance.AspNetCore.Buddy.Npgsql 11.0.0.2

Prefix Reserved
dotnet add package Adliance.AspNetCore.Buddy.Npgsql --version 11.0.0.2
                    
NuGet\Install-Package Adliance.AspNetCore.Buddy.Npgsql -Version 11.0.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Adliance.AspNetCore.Buddy.Npgsql" Version="11.0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Adliance.AspNetCore.Buddy.Npgsql" Version="11.0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Adliance.AspNetCore.Buddy.Npgsql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Adliance.AspNetCore.Buddy.Npgsql --version 11.0.0.2
                    
#r "nuget: Adliance.AspNetCore.Buddy.Npgsql, 11.0.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Adliance.AspNetCore.Buddy.Npgsql@11.0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Adliance.AspNetCore.Buddy.Npgsql&version=11.0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Adliance.AspNetCore.Buddy.Npgsql&version=11.0.0.2
                    
Install as a Cake Tool

Adliance.AspNetCore.Buddy.Npgsql

Adds PostgreSQL support to ASP.NET Core projects that use Entity Framework Core with Npgsql.

Features

Setup library in an ASP.NET project

The package offers the AddDatabase<T> extensions to register a DbContext that uses PostgreSQL.

using Adliance.AspNetCore.Buddy.Npgsql.Extensions;

// ...

// reads the options from the "Database" section
builder.Services.AddDatabase<MyDbContext>(builder.Configuration);

// or from a specific section
builder.Services.AddDatabase<MyDbContext>(builder.Configuration.GetSection("MyDatabase"));

// or with a connection string or a PostgresDatabaseOptions instance
builder.Services.AddDatabase<MyDbContext>("Host=localhost;Database=mydb;Username=postgres;Password=secret");

Configuration (appsettings.json)

{
  "Database": {
    "ConnectionString": "Host=myserver.postgres.database.azure.com;Database=mydb",
    "EnableMigrations": true,
    "CommandTimeoutSeconds": 10,
    "MigrationCommandTimeoutSeconds": 600
  }
}
Setting Default Description
ConnectionString The Npgsql connection string. Without a password, Entra authentication is used.
EnableMigrations true Whether PostgresMigration.Run applies pending migrations.
CommandTimeoutSeconds 10 Timeout for regular commands. Kept short so that an unresponsive database does not hold requests open for long.
MigrationCommandTimeoutSeconds 600 Timeout for each command while migrations are applied.

Entra authentication

If the connection string contains no password, the library authenticates against PostgreSQL with a Microsoft Entra ID access token instead. Tokens are obtained via DefaultAzureCredential, so this works with

  • a managed identity (system- or user-assigned) when running in Azure, and
  • the developer's own account (Visual Studio, Azure CLI, azd, ...) when running locally.

Tokens are refreshed periodically in the background, so connections keep working after a token has expired.

PostgreSQL still needs a role to log in as. If the connection string contains no Username, it is derived from the token:

  • for a managed identity, it is the name of the identity (user-assigned) or of the resource it belongs to, e.g. the App Service (system-assigned),
  • for a user, it is the user principal name, e.g. jane.doe@example.com.

This matches the default name an identity gets when it is added as an Entra administrator or role on the server. Set Username=... explicitly when access is granted via a group or when the role was created with a different name.

To use Entra authentication with a data source you build yourself, or with a credential other than DefaultAzureCredential, call PostgresEntraAuthentication.Configure directly:

var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
if (PostgresEntraAuthentication.IsRequired(connectionString))
{
    PostgresEntraAuthentication.Configure(dataSourceBuilder, new ManagedIdentityCredential(ManagedIdentityId.SystemAssigned));
}
await using var dataSource = dataSourceBuilder.Build();

Migrations

PostgresMigration.Run applies all pending Entity Framework migrations, for example during startup. Failures are logged as critical instead of thrown, so the application still starts and can report its state through the health check.

builder.Services.AddDatabase<MyDbContext>(builder.Configuration);
builder.Services.Configure<PostgresDatabaseOptions>(builder.Configuration.GetSection("Database"));

var app = builder.Build();

await PostgresMigration.Run<MyDbContext, PostgresDatabaseOptions>(app.Services);

Migrations are skipped when EnableMigrations is false or no connection string is configured. If no logger is passed, one is resolved from the service provider.

Health check

AddPostgresCheck<T> adds a health check for a DbContext. It fails when

  • no connection string is configured,
  • the database cannot be reached (including failed Entra authentication), or
  • there are pending migrations, e.g. because applying them on startup failed. The names of the pending migrations are included in the result's data. Pass checkPendingMigrations: false to skip this.
using Adliance.AspNetCore.Buddy.Npgsql.Extensions;

// ...

builder.Services.AddHealthChecks().AddPostgresCheck<MyDbContext>();

// optionally with a name, a failure status, tags and a timeout
builder.Services.AddHealthChecks().AddPostgresCheck<MyDbContext>("database", HealthStatus.Degraded, ["ready"], TimeSpan.FromSeconds(5));

// ...

app.MapHealthChecks("/health");

The check is named after the DbContext type unless a name is given, and it reports Unhealthy unless a different failure status is given.

Hangfire

UsePostgresStorage configures Hangfire to store its jobs in PostgreSQL via Hangfire.PostgreSql. The connection is built from PostgresDatabaseOptions, so Entra authentication works the same way as for the DbContext.

using Adliance.AspNetCore.Buddy.Npgsql.Extensions;
using Hangfire.PostgreSql;

// ...

builder.Services.Configure<PostgresDatabaseOptions>(builder.Configuration.GetSection("Database"));
builder.Services.AddHangfire((serviceProvider, config) => config.UsePostgresStorage<PostgresDatabaseOptions>(serviceProvider));
builder.Services.AddHangfireServer();

// or with a PostgresDatabaseOptions instance
builder.Services.AddHangfire(config => config.UsePostgresStorage(databaseOptions));

// optionally with Hangfire.PostgreSql's own storage options
builder.Services.AddHangfire((serviceProvider, config) => config.UsePostgresStorage<PostgresDatabaseOptions>(
    serviceProvider,
    new PostgreSqlStorageOptions { SchemaName = "jobs" }));

The overload taking an IServiceProvider reads the options from IOptions<TOptions>, so they must be registered as shown above. AddHangfire and AddHangfireServer come from the Hangfire.AspNetCore package, which the application references itself.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.0.2 81 11/30/2026
10.0.0.5 99 11/26/2026
10.0.0.4 90 11/26/2026
10.0.0.3 256 10/25/2026
10.0.0.2 101 10/25/2026
10.0.0.1 106 10/25/2026