# AspireMongoDBEntityFrameworkCoreExtensions Methods

- Package: [Aspire.MongoDB.EntityFrameworkCore](/reference/api/csharp/aspire.mongodb.entityframeworkcore.md)
- Type: [AspireMongoDBEntityFrameworkCoreExtensions](/reference/api/csharp/aspire.mongodb.entityframeworkcore/aspiremongodbentityframeworkcoreextensions.md)
- Kind: `Methods`
- Members: `2`

Extension methods for configuring EntityFrameworkCore DbContext to a MongoDB database

## AddMongoDbContext(IHostApplicationBuilder, string, string?, Action<MongoDBEntityFrameworkCoreSettings>, Action<DbContextOptionsBuilder>)

- Name: `AddMongoDbContext(IHostApplicationBuilder, string, string?, Action<MongoDBEntityFrameworkCoreSettings>, Action<DbContextOptionsBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.MongoDB.EntityFrameworkCore/AspireMongoDBEntityFrameworkCoreExtensions.cs#L43-L93)

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 AspireMongoDBEntityFrameworkCoreExtensions
{
    public static void AddMongoDbContext<TContext>(
        this IHostApplicationBuilder builder,
        string connectionName,
        string? databaseName = null,
        Action<MongoDBEntityFrameworkCoreSettings>? 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.
- `databaseName` (`string?`) `optional`
  The name of the database. If not provided, it will be extracted from the connection string or settings.
- `configureSettings` (`Action<MongoDBEntityFrameworkCoreSettings>`) `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 [MongoDBEntityFrameworkCoreSettings.ConnectionString](/reference/api/csharp/aspire.mongodb.entityframeworkcore/mongodbentityframeworkcoresettings/properties.md#connectionstring) is not provided.

## Remarks

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

## EnrichMongoDbContext(IHostApplicationBuilder, Action<MongoDBEntityFrameworkCoreSettings>)

- Name: `EnrichMongoDbContext(IHostApplicationBuilder, Action<MongoDBEntityFrameworkCoreSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.MongoDB.EntityFrameworkCore/AspireMongoDBEntityFrameworkCoreExtensions.cs#L109-L125)

Configures logging and telemetry for the `EntityFrameworkCore.DbContext`.

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

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `configureSettings` (`Action<MongoDBEntityFrameworkCoreSettings>`) `optional`
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.

## Exceptions

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

## Remarks

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