Skip to content
Docs Try Aspire

Get started with the MongoDB Entity Framework Core integrations

MongoDB logo

MongoDB is a popular, open-source NoSQL document database that offers high performance, scalability, and flexible data modeling. The Aspire MongoDB Entity Framework Core (EF Core) client integration provides a way to connect to existing MongoDB databases, or create new instances from the docker.io/library/mongo container image.

In this introduction, you’ll see how to install and use the Aspire MongoDB Entity Framework Core integrations in a simple configuration. The same hosting integration is used with both the EF Core and the non-EF Core client integrations. If you already have this knowledge, see MongoDB Hosting integration and MongoDB EF Core client integration for full reference details.

To begin, install the Aspire MongoDB Hosting integration in your Aspire AppHost project. This integration allows you to create and manage MongoDB database instances from your Aspire hosting projects:

Aspire CLI — Add Aspire.Hosting.MongoDB package
aspire add mongodb

The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:

Aspire CLI — Example output prompt
Select an integration to add:
> mongodb (Aspire.Hosting.MongoDB)
> Other results listed as selectable options...

Next, in the AppHost project, create instances of MongoDB server and database resources, then pass the database to the consuming client projects:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var mongodb = builder.AddMongoDB("mongodb");
var mydb = mongodb.AddDatabase("mydb");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WaitFor(mydb)
.WithReference(mydb);

Now that the hosting integration is ready, the next step is to install and configure the EF Core client integration in any projects that need to use it.

In each of these consuming client projects, install the Aspire MongoDB EF Core client integration:

.NET CLI — Add Aspire.MongoDB.EntityFrameworkCore package
dotnet add package Aspire.MongoDB.EntityFrameworkCore

In the Program.cs file of your client-consuming project, call the AddMongoDbContext extension method on any IHostApplicationBuilder to register your DbContext subclass for use through the dependency injection container. The method takes a connection name parameter.

C# — Program.cs
builder.AddMongoDbContext<MyDbContext>(connectionName: "mydb");

Now that you’ve added the DbContext to the builder in the consuming project, you can use the MongoDB resource to get and store data. Get the DbContext instance using dependency injection. For example, to retrieve your database context object from an example service define it as a constructor parameter and ensure the ExampleService class is registered with the dependency injection container:

C# — ExampleService.cs
public class ExampleService(MyDbContext context)
{
// Use context to interact with the database...
}

Having obtained the database context, you can work with the MongoDB database as you would in any other C# application using EF Core.

Now that you have an Aspire app with MongoDB EF Core integrations up and running, you can use the following reference documents to learn how to configure and interact with the MongoDB resources: