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
                    
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="Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL" />
                    
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 Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL --version 9.2.0
                    
#r "nuget: Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL, 9.2.0"
                    
#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.
#addin nuget:?package=Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL&version=9.2.0
                    
Install Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL as a Cake Addin
#tool nuget:?package=Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL&version=9.2.0
                    
Install Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL as a Cake Tool

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

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

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 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. 
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
9.2.0 405 7 days ago