# AspireAzureEFCoreCosmosExtensions Methods

- Package: [Aspire.Microsoft.EntityFrameworkCore.Cosmos](/reference/api/csharp/aspire.microsoft.entityframeworkcore.cosmos.md)
- Type: [AspireAzureEFCoreCosmosExtensions](/reference/api/csharp/aspire.microsoft.entityframeworkcore.cosmos/aspireazureefcorecosmosextensions.md)
- Kind: `Methods`
- Members: `3`

Extension methods for configuring EntityFrameworkCore DbContext to Azure Cosmos DB

## AddCosmosDbContext(IHostApplicationBuilder, string, Action<EntityFrameworkCoreCosmosSettings>, Action<DbContextOptionsBuilder>)

- Name: `AddCosmosDbContext(IHostApplicationBuilder, string, Action<EntityFrameworkCoreCosmosSettings>, Action<DbContextOptionsBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs#L42-L65)

Registers the given `EntityFrameworkCore.DbContext` as a service in the services provided by the `builder`. Derives the name of the database from the connection string.

```csharp
public static class AspireAzureEFCoreCosmosExtensions
{
    public static void AddCosmosDbContext<TContext>(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<EntityFrameworkCoreCosmosSettings>? 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<EntityFrameworkCoreCosmosSettings>`) `optional`
  An optional delegate that can be used for customizing settings. 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 [EntityFrameworkCoreCosmosSettings.ConnectionString](/reference/api/csharp/aspire.microsoft.entityframeworkcore.cosmos/entityframeworkcorecosmossettings/properties.md#connectionstring) is not provided.

## AddCosmosDbContext(IHostApplicationBuilder, string, string, Action<EntityFrameworkCoreCosmosSettings>, Action<DbContextOptionsBuilder>)

- Name: `AddCosmosDbContext(IHostApplicationBuilder, string, string, Action<EntityFrameworkCoreCosmosSettings>, Action<DbContextOptionsBuilder>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs#L86-L171)

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

```csharp
public static class AspireAzureEFCoreCosmosExtensions
{
    public static void AddCosmosDbContext<TContext>(
        this IHostApplicationBuilder builder,
        string connectionName,
        string databaseName,
        Action<EntityFrameworkCoreCosmosSettings>? 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`)
  The name of the database to use within the Azure Cosmos DB account.
- `configureSettings` (`Action<EntityFrameworkCoreCosmosSettings>`) `optional`
  An optional delegate that can be used for customizing settings. 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 [EntityFrameworkCoreCosmosSettings.ConnectionString](/reference/api/csharp/aspire.microsoft.entityframeworkcore.cosmos/entityframeworkcorecosmossettings/properties.md#connectionstring) is not provided.

## EnrichCosmosDbContext(IHostApplicationBuilder, Action<EntityFrameworkCoreCosmosSettings>)

- Name: `EnrichCosmosDbContext(IHostApplicationBuilder, Action<EntityFrameworkCoreCosmosSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs#L183-L215)

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

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

## Parameters

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

## Exceptions

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