Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
13.6.1
Prefix Reserved
dotnet add package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL --version 13.6.1
NuGet\Install-Package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL -Version 13.6.1
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="13.6.1" />
<PackageVersion Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="13.6.1" />
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" />
paket add Aspire.Npgsql.EntityFrameworkCore.PostgreSQL --version 13.6.1
#r "nuget: Aspire.Npgsql.EntityFrameworkCore.PostgreSQL, 13.6.1"
#:package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL@13.6.1
#addin nuget:?package=Aspire.Npgsql.EntityFrameworkCore.PostgreSQL&version=13.6.1
#tool nuget:?package=Aspire.Npgsql.EntityFrameworkCore.PostgreSQL&version=13.6.1
Aspire.Npgsql.EntityFrameworkCore.PostgreSQL library
Registers EntityFrameworkCore DbContext in the DI container for connecting PostgreSQL®* database. Enables connection pooling, retries, health check, logging and telemetry.
Getting started
Prerequisites
- PostgreSQL database and connection string for accessing the database.
Install the package
Install the Aspire PostgreSQL EntityFrameworkCore Npgsql library with NuGet:
dotnet add package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
Usage example
In the AppHost.cs file of your project, call the AddNpgsqlDbContext extension method to register a DbContext for use via the dependency injection container. The method takes a connection name parameter.
builder.AddNpgsqlDbContext<MyDbContext>("postgresdb");
You can then retrieve the MyDbContext instance using dependency injection. For example, to retrieve the context from a Web API controller:
private readonly MyDbContext _context;
public ProductsController(MyDbContext context)
{
_context = context;
}
You might also need to configure specific option of Npgsql, or register a DbContext in other ways. In this case call the EnrichNpgsqlDbContext extension method, for example:
var connectionString = builder.Configuration.GetConnectionString("postgresdb");
builder.Services.AddDbContextPool<MyDbContext>(dbContextOptionsBuilder => dbContextOptionsBuilder.UseNpgsql(connectionString));
builder.EnrichNpgsqlDbContext<MyDbContext>();
Configuration
The Aspire PostgreSQL EntityFrameworkCore Npgsql component provides multiple options to configure the database connection based on the requirements and conventions of your project.
Use a connection string
When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddNpgsqlDbContext():
builder.AddNpgsqlDbContext<MyDbContext>("myConnection");
And then the connection string will be retrieved from the ConnectionStrings configuration section:
{
"ConnectionStrings": {
"myConnection": "Host=myserver;Database=test"
}
}
The EnrichNpgsqlDbContext won't make use of the ConnectionStrings configuration section since it expects a DbContext to be registered at the point it is called.
See the ConnectionString documentation for more information on how to format this connection string.
Use configuration providers
The Aspire PostgreSQL EntityFrameworkCore Npgsql component supports Microsoft.Extensions.Configuration. It loads the NpgsqlEntityFrameworkCorePostgreSQLSettings from configuration by using the Aspire:Npgsql:EntityFrameworkCore:PostgreSQL key. Example appsettings.json that configures some of the options:
{
"Aspire": {
"Npgsql": {
"EntityFrameworkCore": {
"PostgreSQL": {
"DisableHealthChecks": true,
"DisableTracing": true
}
}
}
}
}
Use inline delegates
Also you can pass the Action<NpgsqlEntityFrameworkCorePostgreSQLSettings> configureSettings delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddNpgsqlDbContext<MyDbContext>("postgresdb", settings => settings.DisableHealthChecks = true);
or
builder.EnrichNpgsqlDbContext<MyDbContext>(settings => settings.DisableHealthChecks = true);
AppHost extensions
In your AppHost project, install the Aspire.Hosting.PostgreSQL library with NuGet:
dotnet add package Aspire.Hosting.PostgreSQL
Then, in the AppHost.cs file of AppHost, register a Postgres database and consume the connection using the following methods:
var postgresdb = builder.AddPostgres("pg").AddDatabase("postgresdb");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(postgresdb);
The WithReference method configures a connection in the MyService project named postgresdb. In the Program.cs file of MyService, the database connection can be consumed using:
builder.AddNpgsqlDbContext<MyDbContext>("postgresdb");
Additional documentation
- https://learn.microsoft.com/ef/core/
- https://github.com/microsoft/aspire/tree/main/src/Components/README.md
Feedback & contributing
https://github.com/microsoft/aspire
*Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Caching.Memory (>= 10.0.12)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.12)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 10.0.12)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.12)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 10.0.12)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Logging (>= 10.0.12)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options (>= 10.0.12)
- Microsoft.Extensions.Primitives (>= 10.0.12)
- Npgsql.DependencyInjection (>= 10.0.3)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 10.0.3)
- Npgsql.OpenTelemetry (>= 10.0.3)
- OpenTelemetry.Extensions.Hosting (>= 1.17.0)
-
net8.0
- Microsoft.Extensions.Caching.Memory (>= 10.0.12)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.12)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 10.0.12)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.31)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 8.0.31)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Logging (>= 10.0.12)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options (>= 10.0.12)
- Microsoft.Extensions.Primitives (>= 10.0.12)
- Npgsql.DependencyInjection (>= 8.0.9)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 8.0.11)
- Npgsql.OpenTelemetry (>= 8.0.9)
- OpenTelemetry.Extensions.Hosting (>= 1.17.0)
-
net9.0
- Microsoft.Extensions.Caching.Memory (>= 10.0.12)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.12)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 10.0.12)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 9.0.20)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 9.0.20)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Logging (>= 10.0.12)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options (>= 10.0.12)
- Microsoft.Extensions.Primitives (>= 10.0.12)
- Npgsql.DependencyInjection (>= 9.0.5)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.4)
- Npgsql.OpenTelemetry (>= 9.0.5)
- OpenTelemetry.Extensions.Hosting (>= 1.17.0)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on Aspire.Npgsql.EntityFrameworkCore.PostgreSQL:
| Package | Downloads |
|---|---|
|
Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL
An Azure Database for PostgreSQL® provider for Entity Framework Core that integrates with Aspire, including connection pooling, health checks, logging, and telemetry. |
|
|
FlickFind.Common
Package Description |
|
|
Vulthil.SharedKernel.Infrastructure.Npgsql
Provider-specific EF Core mapping and optimizations for Npgsql/PostgreSQL. |
|
|
Core.Cqrs.CommandAndQueryHandler
Package Description |
|
|
CustomDateTimeOffset
Package Description |
GitHub repositories (9)
Showing the top 9 popular GitHub repositories that depend on Aspire.Npgsql.EntityFrameworkCore.PostgreSQL:
| Repository | Stars |
|---|---|
|
jasontaylordev/CleanArchitecture
Clean Architecture Solution Template for ASP.NET Core
|
|
|
dotnet/eShop
A reference .NET application implementing an eCommerce site
|
|
|
ChilliCream/graphql-platform
Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Nitro the awesome Monaco based GraphQL IDE.
|
|
|
thomhurst/TUnit
A modern, fast and flexible .NET testing framework
|
|
|
microsoft/kernel-memory
Research project. A Memory solution for users, teams, and applications.
|
|
|
abpframework/abp-samples
Sample solutions built with the ABP Framework
|
|
|
microsoft/aspire-samples
Browse the sample apps demonstrating Aspire integration across C#, JavaScript, TypeScript, Python, Go, containers, databases, cloud, AI, and observability scenarios.
|
|
|
tonybaloney/CSnakes
Embed Python in .NET
|
|
|
mehdihadeli/food-delivery-microservices
🍔 A practical and cloud-native food delivery microservices, built with .Net Aspire, .Net 10, Wolverine, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 13.6.1 | 6,690 | 10/7/2026 |
| 13.6.0 | 50,678 | 9/29/2026 |
| 13.5.4 | 113,427 | 9/15/2026 |
| 13.5.3 | 194,914 | 8/25/2026 |
| 13.5.2 | 42,121 | 8/21/2026 |
| 13.5.1 | 23,587 | 8/20/2026 |
| 13.5.0 | 28,754 | 8/18/2026 |
| 13.4.6 | 658,167 | 6/19/2026 |
| 13.4.5 | 43,199 | 6/17/2026 |
| 13.4.4 | 28,660 | 6/15/2026 |
| 13.4.3 | 74,092 | 6/8/2026 |
| 13.4.2 | 48,360 | 6/3/2026 |
| 13.4.1 | 4,788 | 6/3/2026 |
| 13.4.0 | 97,742 | 6/1/2026 |
| 13.3.5 | 157,643 | 5/21/2026 |
| 13.3.4 | 21,183 | 5/19/2026 |
| 13.3.3 | 55,694 | 5/15/2026 |
| 13.3.2 | 22,751 | 5/14/2026 |
| 13.3.1 | 16,968 | 5/12/2026 |
| 13.3.0 | 90,826 | 5/7/2026 |