Aspire.MongoDB.EntityFrameworkCore 13.2.2

Prefix Reserved
dotnet add package Aspire.MongoDB.EntityFrameworkCore --version 13.2.2
                    
NuGet\Install-Package Aspire.MongoDB.EntityFrameworkCore -Version 13.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aspire.MongoDB.EntityFrameworkCore" Version="13.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aspire.MongoDB.EntityFrameworkCore" Version="13.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Aspire.MongoDB.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Aspire.MongoDB.EntityFrameworkCore --version 13.2.2
                    
#r "nuget: Aspire.MongoDB.EntityFrameworkCore, 13.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Aspire.MongoDB.EntityFrameworkCore@13.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Aspire.MongoDB.EntityFrameworkCore&version=13.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Aspire.MongoDB.EntityFrameworkCore&version=13.2.2
                    
Install as a Cake Tool

Aspire.MongoDB.EntityFrameworkCore library

Registers EntityFrameworkCore DbContext in the DI container for connecting to a MongoDB database. Enables connection pooling, health check, logging and telemetry.

Getting started

Prerequisites

  • MongoDB database and connection string for accessing the database.

Install the package

Install the Aspire MongoDB EntityFrameworkCore library with NuGet:

dotnet add package Aspire.MongoDB.EntityFrameworkCore

Usage example

In the Program.cs file of your project, call the AddMongoDbContext extension method to register a DbContext for use via the dependency injection container. The method takes a connection name parameter, and optionally a database name.

builder.AddMongoDbContext<MyDbContext>("mongodb", "mydb");

The database name can be omitted if it's included in the connection string or configured via settings:

builder.AddMongoDbContext<MyDbContext>("mongodb");

You can then retrieve the MyDbContext instance using dependency injection. For example, to retrieve the context from a Web API controller:

private readonly MyDbContext _context;

public ProductsController(MyDbContext context)
{
    _context = context;
}

You might also need to configure specific options of MongoDB, or register a DbContext in other ways. In this case call the EnrichMongoDbContext extension method, for example:

var connectionString = builder.Configuration.GetConnectionString("mongodb");
builder.Services.AddDbContextPool<MyDbContext>(dbContextOptionsBuilder => dbContextOptionsBuilder.UseMongoDB(connectionString, "mydb"));
builder.EnrichMongoDbContext<MyDbContext>();

Configuration

The Aspire MongoDB EntityFrameworkCore component provides multiple options to configure the database 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.AddMongoDbContext():

builder.AddMongoDbContext<MyDbContext>("myConnection", "mydb");

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

{
  "ConnectionStrings": {
    "myConnection": "mongodb://server:port"
  }
}

If your connection string includes the database name, you can omit the databaseName parameter:

{
  "ConnectionStrings": {
    "myConnection": "mongodb://server:port/mydb"
  }
}
builder.AddMongoDbContext<MyDbContext>("myConnection");

The EnrichMongoDbContext won't make use of the ConnectionStrings configuration section since it expects a DbContext to be registered at the point it is called.

See the ConnectionString documentation for more information on how to format this connection string.

Use configuration providers

The Aspire MongoDB EntityFrameworkCore component supports Microsoft.Extensions.Configuration. It loads the MongoDBEntityFrameworkCoreSettings from configuration by using the Aspire:MongoDB:EntityFrameworkCore key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "MongoDB": {
      "EntityFrameworkCore": {
        "DatabaseName": "mydb",
        "DisableHealthChecks": true,
        "DisableTracing": false
      }
    }
  }
}

Use inline delegates

Also you can pass the Action<MongoDBEntityFrameworkCoreSettings> configureSettings delegate to set up some or all the options inline, for example to disable health checks from code:

builder.AddMongoDbContext<MyDbContext>("mongodb", "mydb", settings => settings.DisableHealthChecks = true);

or

builder.EnrichMongoDbContext<MyDbContext>(settings => settings.DisableHealthChecks = true);

AppHost extensions

In your AppHost project, install the Aspire.Hosting.MongoDB library with NuGet:

dotnet add package Aspire.Hosting.MongoDB

Then, in the AppHost.cs file of AppHost, register a MongoDB database and consume the connection using the following methods:

var mongodb = builder.AddMongoDB("mongodb").AddDatabase("mydatabase");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(mongodb);

The WithReference method configures a connection in the MyService project named mydatabase. In the Program.cs file of MyService, the database connection can be consumed using:

builder.AddMongoDbContext<MyDbContext>("mydatabase");

Note: When using AddDatabase in the AppHost, the database name is included in the generated connection string, so you don't need to specify it again in AddMongoDbContext.

Additional documentation

Feedback & contributing

https://github.com/microsoft/aspire

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
13.2.2 97 4/8/2026
13.2.1 99 3/31/2026
13.2.0 132 3/23/2026