.NET Aspire Microsoft Entity Framework Core Cosmos DB component

In this article, you learn how to use the .NET Aspire Microsoft Entity Framework Core Cosmos DB component. The Aspire.Microsoft.EntityFrameworkCore.Cosmos library is used to register a System.Data.Entity.DbContext as a singleton in the DI container for connecting to Azure Cosmos DB. It also enables corresponding health checks, logging and telemetry.

Get started

To get started with the .NET Aspire Microsoft Entity Framework Core Cosmos DB component, install the Aspire.Microsoft.EntityFrameworkCore.Cosmos NuGet package.

dotnet add package Aspire.Microsoft.EntityFrameworkCore.Cosmos --prerelease

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your component-consuming project, call the AddCosmosDbContext extension to register a System.Data.Entity.DbContext for use via the dependency injection container.

builder.AddCosmosDbContext<MyDbContext>("cosmosdb");

You can then retrieve the DbContext instance using dependency injection. For example, to retrieve the client from a service:

public class ExampleService(MyDbContext context)
{
    // Use context...
}

For more information on using Entity Framework Core with Azure Cosmos DB, see the Examples for Azure Cosmos DB for NoSQL SDK for .NET.

App host usage

To add Azure Cosmos DB hosting support to your IDistributedApplicationBuilder, install the Aspire.Hosting.Azure.CosmosDB NuGet package.

dotnet add package Aspire.Hosting.Azure.CosmosDB --prerelease

In your app host project, register the .NET Aspire Microsoft Entity Framework Core Cosmos DB component and consume the service using the following methods:

// Service registration
var cosmosdbService = builder.AddAzureCosmosDB("cdb")
                             .AddDatabase("cosmosdb");

// Service consumption
var exampleProject = builder.AddProject<Projects.ExampleProject>()
                            .WithReference(cosmosdbService);

The AddAzureCosmosDB method will read connection information from the AppHost's configuration under the ConnectionStrings:cosmosdb config key. The WithReference method passes that connection information into a connection string named cosmosdb in the ExampleProject project. In the Program.cs file of cosmosdbService, the connection can be consumed using:

builder.AddAzureCosmosDB("cosmosdb");

Configuration

The .NET Aspire Microsoft Entity Framework Core Cosmos DB component provides multiple options to configure the Azure Cosmos DB connection based on the requirements and conventions of your project.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddCosmosDbContext:

builder.AddCosmosDbContext<MyDbContext>("CosmosConnection");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "CosmosConnection": "AccountEndpoint=https://{account_name}.documents.azure.com:443/;AccountKey={account_key};"
  }
}

For more information, see the ConnectionString documentation.

Use configuration providers

The .NET Aspire Microsoft Entity Framework Core Cosmos DB component supports Microsoft.Extensions.Configuration. It loads the AzureCosmosDBSettings from appsettings.json or other configuration files using Aspire:Microsoft:EntityFrameworkCore:Cosmos key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Microsoft": {
      "EntityFrameworkCore": {
        "Cosmos": {
          "DbContextPooling": true,
          "Tracing": false
        }
      }
    }
  }
}

Use inline delegates

You can also pass the Action<EntityFrameworkCoreCosmosDBSettings> configureSettings delegate to set up some or all the EntityFrameworkCoreCosmosDBSettings options inline, for example to disable tracing from code:

builder.AddCosmosDbContext<MyDbContext>(
    "cosmosdb",
    settings => settings.Tracing = false);

Health checks

By default, .NET Aspire components enable health checks for all services. For more information, see .NET Aspire components overview.

The .NET Aspire Microsoft Entity Framework Core Cosmos DB component currently doesn't implement health checks, though this may change in future releases.

Observability and telemetry

.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire Microsoft Entity Framework Core Cosmos DB component uses the following log categories:

  • Azure-Cosmos-Operation-Request-Diagnostics
  • Microsoft.EntityFrameworkCore.ChangeTracking
  • Microsoft.EntityFrameworkCore.Database.Command
  • Microsoft.EntityFrameworkCore.Infrastructure
  • Microsoft.EntityFrameworkCore.Query

Tracing

The .NET Aspire Microsoft Entity Framework Core Cosmos DB component will emit the following tracing activities using OpenTelemetry:

  • Azure.Cosmos.Operation
  • OpenTelemetry.Instrumentation.EntityFrameworkCore

Metrics

The .NET Aspire Microsoft Entity Framework Core Cosmos DB component currently supports the following metrics:

  • Microsoft.EntityFrameworkCore"
    • ec_Microsoft_EntityFrameworkCore_active_db_contexts
    • ec_Microsoft_EntityFrameworkCore_total_queries
    • ec_Microsoft_EntityFrameworkCore_queries_per_second
    • ec_Microsoft_EntityFrameworkCore_total_save_changes
    • ec_Microsoft_EntityFrameworkCore_save_changes_per_second
    • ec_Microsoft_EntityFrameworkCore_compiled_query_cache_hit_rate
    • ec_Microsoft_Entity_total_execution_strategy_operation_failures
    • ec_Microsoft_E_execution_strategy_operation_failures_per_second
    • ec_Microsoft_EntityFramew_total_optimistic_concurrency_failures
    • ec_Microsoft_EntityF_optimistic_concurrency_failures_per_second

See also