MongoDB Entity Framework Core client integration
To get started with the Aspire MongoDB Entity Framework Core (EF Core) client integration, install the 📦 Aspire.MongoDB.EntityFrameworkCore NuGet package in the client-consuming project, that is, the project for the application that uses the MongoDB client. The Aspire MongoDB EF Core client integration registers your desired DbContext subclass instances that you can use to interact with MongoDB.
dotnet add package Aspire.MongoDB.EntityFrameworkCore#:package Aspire.MongoDB.EntityFrameworkCore@*<PackageReference Include="Aspire.MongoDB.EntityFrameworkCore" Version="*" />For an introduction to the MongoDB EF Core integration, see Get started with the MongoDB Entity Framework Core integrations.
Add MongoDB database context
Section titled “Add MongoDB database context”In the Program.cs file of your client-consuming project, call the AddMongoDbContext extension method on any IHostApplicationBuilder to register your Microsoft.EntityFrameworkCore.DbContext subclass for use via the dependency injection container. The method takes a connection name parameter, and optionally a database name.
builder.AddMongoDbContext<MyDbContext>( connectionName: "mydb");After adding MyDbContext to the builder, you can get the MyDbContext instance using dependency injection. For example, to retrieve your data source object from an example service, define it as a constructor parameter and ensure the ExampleService class is registered with the dependency injection container:
public class ExampleService(MyDbContext context){ // Use context...}For more information on dependency injection, see .NET dependency injection.
Specify database name
Section titled “Specify database name”MongoDB connection strings don’t always include a database name, so you may need to provide one. The database name is resolved in the following order:
- Explicit parameter: Pass
databaseNameas the second parameter toAddMongoDbContext. - Configuration: Set the
DatabaseNameproperty in theAspire:MongoDB:EntityFrameworkCoreconfiguration section. - Connection string: If the connection string includes a database name (e.g.,
mongodb://server:port/mydb), it’s extracted automatically.
builder.AddMongoDbContext<MyDbContext>("mongodb", "mydb");Enrich a MongoDB database context
Section titled “Enrich a MongoDB database context”You may prefer to use the standard EF Core method to obtain a database context and add it to the dependency injection container:
builder.Services.AddDbContextPool<MyDbContext>(options => options.UseMongoDB( builder.Configuration.GetConnectionString("mydb")!, "mydb"));You have more flexibility when you create the database context in this way, for example:
- You can reuse existing configuration code for the database context without rewriting it for Aspire.
- You can use Entity Framework Core interceptors to modify database operations.
- You can choose not to use Entity Framework Core context pooling, which may perform better in some circumstances.
If you use this method, you can enhance the database context with Aspire-style health checks, logging, and telemetry features by calling the EnrichMongoDbContext method:
builder.EnrichMongoDbContext<MyDbContext>( configureSettings: settings => { settings.DisableHealthChecks = false; settings.DisableTracing = false; });The settings parameter is an instance of the MongoDBEntityFrameworkCoreSettings class.
Configuration
Section titled “Configuration”The Aspire MongoDB EF Core integration provides multiple configuration approaches and options to meet the requirements and conventions of your project.
Use a connection string
Section titled “Use a connection string”When using a connection string from the ConnectionStrings configuration section, you provide the name of the connection string when calling the AddMongoDbContext method:
builder.AddMongoDbContext<MyDbContext>("mydb");The connection string is retrieved from the ConnectionStrings configuration section:
{ "ConnectionStrings": { "mydb": "mongodb://server:port/mydb" }}If your connection string doesn’t include a database name, you can provide it as the second parameter:
builder.AddMongoDbContext<MyDbContext>("mongodb", "mydb");The EnrichMongoDbContext won’t make use of the ConnectionStrings configuration section since it expects a DbContext to be registered at the point it’s called.
For more information, see the MongoDB connection string documentation.
Use configuration providers
Section titled “Use configuration providers”The Aspire MongoDB EF Core integration supports Microsoft.Extensions.Configuration. It loads the MongoDBEntityFrameworkCoreSettings from configuration files such as appsettings.json by using the Aspire:MongoDB:EntityFrameworkCore key. If you have set up your configurations in the Aspire:MongoDB:EntityFrameworkCore section you can just call the method without passing any parameter.
The following example shows an appsettings.json file that configures some of the available options:
{ "Aspire": { "MongoDB": { "EntityFrameworkCore": { "DatabaseName": "mydb", "DisableHealthChecks": true, "DisableTracing": false, "HealthCheckTimeout": 5000 } } }}For the complete MongoDB EF Core client integration JSON schema, see Aspire.MongoDB.EntityFrameworkCore/ConfigurationSchema.json.
Use inline delegates
Section titled “Use inline delegates”You can also pass the Action<MongoDBEntityFrameworkCoreSettings> delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddMongoDbContext<MyDbContext>( "mydb", configureSettings: static settings => settings.DisableHealthChecks = true);or
builder.EnrichMongoDbContext<MyDbContext>( settings => settings.DisableHealthChecks = true);Configure multiple DbContext classes
Section titled “Configure multiple DbContext classes”If you want to register more than one DbContext with different configuration, you can use $"Aspire:MongoDB:EntityFrameworkCore:{typeof(TContext).Name}" configuration section name. The json configuration would look like:
{ "Aspire": { "MongoDB": { "EntityFrameworkCore": { "ConnectionString": "mongodb://server:port/mydb", "DatabaseName": "mydb", "DisableHealthChecks": true, "AnotherDbContext": { "ConnectionString": "mongodb://server:port/anotherdb", "DatabaseName": "anotherdb", "DisableTracing": false } } } }}Then calling the AddMongoDbContext method with AnotherDbContext type parameter would load the settings from AnotherDbContext section.
builder.AddMongoDbContext<AnotherDbContext>("mongodb");MongoDB Driver integration vs. EF Core integration
Section titled “MongoDB Driver integration vs. EF Core integration”The Aspire MongoDB integration comes in two flavors:
- Aspire.MongoDB.Driver: Provides direct access to the MongoDB driver via
IMongoClient. Use this when you want full control over MongoDB queries, need features not supported by EF Core (such as aggregation pipelines), or prefer the native MongoDB programming model. - Aspire.MongoDB.EntityFrameworkCore (this page): Provides an EF Core
DbContextbacked by MongoDB. Use this when you want to leverage EF Core’s familiar patterns—change tracking, LINQ queries, andDbSet<T>abstractions—with MongoDB as the data store.
Both integrations use the same hosting integration (Aspire.Hosting.MongoDB) in the AppHost.
Health checks and observability
Section titled “Health checks and observability”By default, the Aspire MongoDB EF Core integration handles the following:
- Adds the
DbContextHealthCheck, which calls EF Core’sCanConnectAsyncmethod. The name of the health check is the name of theTContexttype. - Integrates with the
/healthHTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic.
Logging
Section titled “Logging”The Aspire MongoDB Entity Framework Core integration uses the following log categories:
Microsoft.EntityFrameworkCore.ChangeTrackingMicrosoft.EntityFrameworkCore.Database.CommandMicrosoft.EntityFrameworkCore.Database.ConnectionMicrosoft.EntityFrameworkCore.Database.TransactionMicrosoft.EntityFrameworkCore.InfrastructureMicrosoft.EntityFrameworkCore.MigrationsMicrosoft.EntityFrameworkCore.ModelMicrosoft.EntityFrameworkCore.Model.ValidationMicrosoft.EntityFrameworkCore.QueryMicrosoft.EntityFrameworkCore.Update
Tracing
Section titled “Tracing”The Aspire MongoDB EF Core integration will emit the following tracing activities using OpenTelemetry:
MongoDB.Driver.Core.Extensions.DiagnosticSources
Metrics
Section titled “Metrics”The Aspire MongoDB EF Core integration will emit the following metrics using OpenTelemetry:
- Microsoft.EntityFrameworkCore:
ec_Microsoft_EntityFrameworkCore_active_db_contextsec_Microsoft_EntityFrameworkCore_total_queriesec_Microsoft_EntityFrameworkCore_queries_per_secondec_Microsoft_EntityFrameworkCore_total_save_changesec_Microsoft_EntityFrameworkCore_save_changes_per_secondec_Microsoft_EntityFrameworkCore_compiled_query_cache_hit_rateec_Microsoft_Entity_total_execution_strategy_operation_failuresec_Microsoft_E_execution_strategy_operation_failures_per_secondec_Microsoft_EntityFramew_total_optimistic_concurrency_failuresec_Microsoft_EntityF_optimistic_concurrency_failures_per_second