Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL
9.2.0
Prefix Reserved
dotnet add package Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL --version 9.2.0
NuGet\Install-Package Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL -Version 9.2.0
<PackageReference Include="Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.2.0" />
<PackageVersion Include="Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.2.0" />
<PackageReference Include="Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL" />
paket add Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL --version 9.2.0
#r "nuget: Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL, 9.2.0"
#addin nuget:?package=Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL&version=9.2.0
#tool nuget:?package=Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL&version=9.2.0
Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL library
Registers EntityFrameworkCore DbContext in the DI container for connecting to PostgreSQL® and Azure Database for PostgreSQL®. Enables connection pooling, retries, health check, logging and telemetry.
Getting started
Prerequisites
- PostgreSQL database and connection string for accessing the database.
- or an Azure Database for PostgreSQL instance, learn more about how to Create an Azure Database for PostgreSQL resource.
Differences with Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
The Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL library is a wrapper around the Aspire.Npgsql.EntityFrameworkCore.PostgreSQL library that provides additional features for connecting to Azure Database for PostgreSQL. If you don't need these features, you can use the Aspire.Npgsql.EntityFrameworkCore.PostgreSQL library instead. At runtime the client integration will detect whether the connection string has a Username and Password, and if not, it will use Entra Id to authenticate with Azure Database for PostgreSQL.
Install the package
Install the .NET Aspire Azure PostgreSQL EntityFrameworkCore Npgsql library with NuGet:
dotnet add package Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL
Usage example
In the Program.cs file of your project, call the AddAzureNpgsqlDbContext
extension method to register a DbContext
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddAzureNpgsqlDbContext<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 EnrichAzureNpgsqlDbContext
extension method, for example:
var connectionString = builder.Configuration.GetConnectionString("postgresdb");
builder.Services.AddDbContextPool<MyDbContext>(dbContextOptionsBuilder => dbContextOptionsBuilder.UseNpgsql(connectionString));
builder.EnrichAzureNpgsqlDbContext<MyDbContext>();
Configuration
The .NET Aspire Azure 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.AddAzureNpgsqlDbContext()
:
builder.AddAzureNpgsqlDbContext<MyDbContext>("myConnection");
And then the connection string will be retrieved from the ConnectionStrings
configuration section:
{
"ConnectionStrings": {
"myConnection": "Host=myserver;Database=test"
}
}
The EnrichAzureNpgsqlDbContext
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 .NET Aspire PostgreSQL EntityFrameworkCore Npgsql component supports Microsoft.Extensions.Configuration. It loads the AzureNpgsqlEntityFrameworkCorePostgreSQLSettings
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<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings> configureSettings
delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddAzureNpgsqlDbContext<MyDbContext>("postgresdb", settings => settings.DisableHealthChecks = true);
or
builder.EnrichAzureNpgsqlDbContext<MyDbContext>(settings => settings.DisableHealthChecks = true);
Use the AzureNpgsqlEntityFrameworkCorePostgreSQLSettings.Credential
property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.
If the connection string contains a username and a password then the credential will be ignored.
AppHost extensions
In your AppHost project, install the Aspire.Hosting.Azure.PostgreSQL
library with NuGet:
dotnet add package Aspire.Hosting.Azure.PostgreSQL
Then, in the Program.cs file of AppHost
, register Azure Database for PostgreSQL instance and consume the connection using the following methods:
var postgresdb = builder.AddAzurePostgresFlexibleServer("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.AddAzureNpgsqlDbContext<MyDbContext>("postgresdb");
Troubleshooting
In the rare case that the Username property is not provided and the integration can't detect it using the application's Managed Identity, Npgsql will throw an exception like the following:
Npgsql.PostgresException (0x80004005): 28P01: password authentication failed for user ...
In that case you can configure the Username property in the connection string and use EnrichAzureNpgsqlDbContext
, passing the connection string in UseNpgsql
:
builder.Services.AddDbContextPool<MyDbContext>(options => options.UseNpgsql(newConnectionString));
builder.EnrichAzureNpgsqlDbContext<MyDbContext>();
Additional documentation
- https://learn.microsoft.com/ef/core/
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
Feedback & contributing
https://github.com/dotnet/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. |
-
net8.0
- Aspire.Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.2.0)
- Azure.Core (>= 1.44.1)
- Azure.Identity (>= 1.13.2)
- Microsoft.Extensions.Azure (>= 1.11.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.15)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 8.0.15)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Primitives (>= 8.0.0)
- Npgsql.DependencyInjection (>= 8.0.6)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 8.0.11)
- Npgsql.OpenTelemetry (>= 8.0.6)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
- System.Text.Json (>= 8.0.5)
-
net9.0
- Aspire.Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.2.0)
- Azure.Core (>= 1.44.1)
- Azure.Identity (>= 1.13.2)
- Microsoft.Extensions.Azure (>= 1.11.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 9.0.4)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 9.0.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Options (>= 9.0.4)
- Microsoft.Extensions.Primitives (>= 9.0.4)
- Npgsql.DependencyInjection (>= 9.0.3)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.4)
- Npgsql.OpenTelemetry (>= 9.0.2)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
- System.Text.Json (>= 9.0.4)
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 |
---|---|---|
9.2.0 | 405 | 7 days ago |