# AspireAzureEFPostgreSqlExtensions Methods

- Package: [Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL](/reference/api/csharp/aspire.azure.npgsql.entityframeworkcore.postgresql.md)
- Type: [AspireAzureEFPostgreSqlExtensions](/reference/api/csharp/aspire.azure.npgsql.entityframeworkcore.postgresql/aspireazureefpostgresqlextensions.md)
- Kind: `Methods`
- Members: `2`

Provides extension methods for registering a PostgreSQL database context in an Aspire application.

## AddAzureNpgsqlDbContext(IHostApplicationBuilder, string, Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>, Action<DbContextOptionsBuilder>)

- Name: `AddAzureNpgsqlDbContext(IHostApplicationBuilder, string, Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>, Action<DbContextOptionsBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL/AspireAzureEFPostgreSqlExtensions.cs#L52-L65)

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.

```csharp
public static class AspireAzureEFPostgreSqlExtensions
{
    public static void AddAzureNpgsqlDbContext<TContext>(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>? configureSettings = null,
        Action<DbContextOptionsBuilder>? configureDbContextOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  A name used to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>`) `optional`
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.
- `configureDbContextOptions` (`Action<DbContextOptionsBuilder>`) `optional`
  An optional delegate to configure the `EntityFrameworkCore.DbContextOptions` for the context.

## Exceptions

- `ArgumentNullException` -- Thrown if mandatory `builder` is null.
- `InvalidOperationException` -- Thrown when mandatory `NpgsqlEntityFrameworkCorePostgreSQLSettings.ConnectionString` is not provided.

## Remarks

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>)

- Name: `EnrichAzureNpgsqlDbContext(IHostApplicationBuilder, Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL/AspireAzureEFPostgreSqlExtensions.cs#L77-L94)

Configures retries, health check, logging and telemetry for the `EntityFrameworkCore.DbContext`.

```csharp
public static class AspireAzureEFPostgreSqlExtensions
{
    public static void EnrichAzureNpgsqlDbContext<TContext>(
        this IHostApplicationBuilder builder,
        Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>? configureSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `configureSettings` (`Action<AzureNpgsqlEntityFrameworkCorePostgreSQLSettings>`) `optional`

## Exceptions

- `ArgumentNullException` -- Thrown if mandatory `builder` is null.
- `InvalidOperationException` -- Thrown when mandatory `EntityFrameworkCore.DbContext` is not registered in DI.
