# AspireSqlServerEFCoreSqlClientExtensions Methods

- Package: [Aspire.Microsoft.EntityFrameworkCore.SqlServer](/reference/api/csharp/aspire.microsoft.entityframeworkcore.sqlserver.md)
- Type: [AspireSqlServerEFCoreSqlClientExtensions](/reference/api/csharp/aspire.microsoft.entityframeworkcore.sqlserver/aspiresqlserverefcoresqlclientextensions.md)
- Kind: `Methods`
- Members: `2`

Extension methods for configuring EntityFrameworkCore DbContext to Azure SQL, MS SQL server

## AddSqlServerDbContext(IHostApplicationBuilder, string, Action<MicrosoftEntityFrameworkCoreSqlServerSettings>, Action<DbContextOptionsBuilder>)

- Name: `AddSqlServerDbContext(IHostApplicationBuilder, string, Action<MicrosoftEntityFrameworkCoreSqlServerSettings>, Action<DbContextOptionsBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.EntityFrameworkCore.SqlServer/AspireSqlServerEFCoreSqlClientExtensions.cs#L43-L89)

Registers the given `EntityFrameworkCore.DbContext` as a service in the services provided by the `builder`. Enables db context pooling, retries, health check, logging and telemetry for the `EntityFrameworkCore.DbContext`.

```csharp
public static class AspireSqlServerEFCoreSqlClientExtensions
{
    public static void AddSqlServerDbContext<TContext>(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<MicrosoftEntityFrameworkCoreSqlServerSettings>? 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<MicrosoftEntityFrameworkCoreSqlServerSettings>`) `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 [MicrosoftEntityFrameworkCoreSqlServerSettings.ConnectionString](/reference/api/csharp/aspire.microsoft.entityframeworkcore.sqlserver/microsoftentityframeworkcoresqlserversettings/properties.md#connectionstring) is not provided.

## Remarks

Reads the configuration from "Aspire:Microsoft:EntityFrameworkCore:SqlServer:{typeof(TContext).Name}" config section, or "Aspire:Microsoft:EntityFrameworkCore:SqlServer" if former does not exist.

## EnrichSqlServerDbContext(IHostApplicationBuilder, Action<MicrosoftEntityFrameworkCoreSqlServerSettings>)

- Name: `EnrichSqlServerDbContext(IHostApplicationBuilder, Action<MicrosoftEntityFrameworkCoreSqlServerSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.EntityFrameworkCore.SqlServer/AspireSqlServerEFCoreSqlClientExtensions.cs#L100-L174)

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

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

## Parameters

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

## Exceptions

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