# AspireEFMySqlExtensions Methods

- Package: [Aspire.Pomelo.EntityFrameworkCore.MySql](/reference/api/csharp/aspire.pomelo.entityframeworkcore.mysql.md)
- Type: [AspireEFMySqlExtensions](/reference/api/csharp/aspire.pomelo.entityframeworkcore.mysql/aspireefmysqlextensions.md)
- Kind: `Methods`
- Members: `2`

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

## AddMySqlDbContext(IHostApplicationBuilder, string, Action<PomeloEntityFrameworkCoreMySqlSettings>, Action<DbContextOptionsBuilder>)

- Name: `AddMySqlDbContext(IHostApplicationBuilder, string, Action<PomeloEntityFrameworkCoreMySqlSettings>, Action<DbContextOptionsBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Pomelo.EntityFrameworkCore.MySql/AspireEFMySqlExtensions.cs#L56-L142)

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 AspireEFMySqlExtensions
{
    public static void AddMySqlDbContext<TContext>(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<PomeloEntityFrameworkCoreMySqlSettings>? 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<PomeloEntityFrameworkCoreMySqlSettings>`) `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 [PomeloEntityFrameworkCoreMySqlSettings.ConnectionString](/reference/api/csharp/aspire.pomelo.entityframeworkcore.mysql/pomeloentityframeworkcoremysqlsettings/properties.md#connectionstring) is not provided.

## Remarks

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

The `DbContext.OnConfiguring` method can then be overridden to configure `EntityFrameworkCore.DbContext` options.

## EnrichMySqlDbContext(IHostApplicationBuilder, Action<PomeloEntityFrameworkCoreMySqlSettings>)

- Name: `EnrichMySqlDbContext(IHostApplicationBuilder, Action<PomeloEntityFrameworkCoreMySqlSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Pomelo.EntityFrameworkCore.MySql/AspireEFMySqlExtensions.cs#L154-L239)

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

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

## Parameters

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

## Exceptions

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