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
<PackageReference Include="Aspire.MongoDB.EntityFrameworkCore" Version="13.2.2" />
<PackageVersion Include="Aspire.MongoDB.EntityFrameworkCore" Version="13.2.2" />
<PackageReference Include="Aspire.MongoDB.EntityFrameworkCore" />
paket add Aspire.MongoDB.EntityFrameworkCore --version 13.2.2
#r "nuget: Aspire.MongoDB.EntityFrameworkCore, 13.2.2"
#:package Aspire.MongoDB.EntityFrameworkCore@13.2.2
#addin nuget:?package=Aspire.MongoDB.EntityFrameworkCore&version=13.2.2
#tool nuget:?package=Aspire.MongoDB.EntityFrameworkCore&version=13.2.2
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
- https://learn.microsoft.com/ef/core/
- https://www.mongodb.com/docs/entity-framework/current/
- https://github.com/microsoft/aspire/tree/main/src/Components/README.md
Feedback & contributing
| Product | Versions 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. |
-
net8.0
- DnsClient (>= 1.8.0)
- Microsoft.Extensions.Caching.Memory (>= 10.0.5)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.25)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 8.0.25)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
- Microsoft.Extensions.Primitives (>= 10.0.5)
- MongoDB.Driver (>= 3.6.0)
- MongoDB.Driver.Core.Extensions.DiagnosticSources (>= 3.0.0)
- MongoDB.EntityFrameworkCore (>= 8.2.3)
- OpenTelemetry.Extensions.Hosting (>= 1.15.0)
- Polly.Core (>= 8.6.5)
- Polly.Extensions (>= 8.6.5)
-
net9.0
- DnsClient (>= 1.8.0)
- Microsoft.Extensions.Caching.Memory (>= 10.0.5)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 9.0.14)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 9.0.14)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
- Microsoft.Extensions.Primitives (>= 10.0.5)
- MongoDB.Driver (>= 3.3.0)
- MongoDB.Driver.Core.Extensions.DiagnosticSources (>= 3.0.0)
- MongoDB.EntityFrameworkCore (>= 9.0.0)
- OpenTelemetry.Extensions.Hosting (>= 1.15.0)
- Polly.Core (>= 8.6.5)
- Polly.Extensions (>= 8.6.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.