Using Mongo DB EF Core provider in .NET Aspire
July 01, 2024 by Anuraj
AspNetCore DotNet MongoDB EFCore
In this blog post, we’ll learn how we can use Mongo DB EF Core provider in .NET Aspire. Long back I wrote a blog post on how to use Mongo database in .NET Aspire project and how to use Mongo DB EF Core provider. By default in .NET Aspire the IMongoClient
interface will be injected in ASP.NET Core which we can be used in controllers and service to interact with Mongo Database.
Creating .NET Aspire starter project
First we will be creating a .NET Aspire starter project with the command dotnet new aspire-starter --output AspireDemo --name AspireDemo
. This command will create four projects. In this solution, the AspireDemo.ApiService
is the API project, AspireDemo.Web
is blazor web app which is the frontend application. The other two projects - AspireDemo.AppHost
and AspireDemo.ServiceDefaults
are part of the Aspire framework. The AspireDemo.AppHost
project is the start up project and all the services and resources will be configured in this project.
Configuring MongoDB to Aspire project
To configure MongoDB to the AspireDemo.AppHost
we need to add the Aspire.Hosting.MongoDB
nuget package to the AspireDemo.AppHost
project. After adding the NuGet package, we need to add the following code.
The above code will create a Mongo resource name mongo
with mongo-data
as data volume. The WithMongoExpress()
method will help us to explore the Mongo database. And the mongo database with name AspireDatabase
.
And the Mongo service is referenced to the apiservice
which we will be able to connect to Mongo database.
Configuring Mongo EF Core to Api service
First we need to configure Aspire.MongoDB.Driver
to the Api service, which is required to configure dependency injection of IMongoClient
. Next we can configure the DbContext and model classes. In the Program.cs
class, we need to write the following code which is used to inject the IMongoClient
interface and this interface is used to interact with Mongo Database.
Here is the Aspire dashboard.
Next we need to create DbContext
class and Movie
model class, like this.
We can create the DbContext class like this.
And we need to configure the DbContext in the Api service, like this.
Right now if we run the application, we will get an error because the AspireDemoDbContext
and IMongoClient
is not associated. To fix this, we need to modify the DbContext class like this.
Now we can run the application and create Movies and get movies from the database. So here is the updated Program.cs
.
This way we can use Mongo EF Core provider in .NET Aspire. Explore it and let me know if you have any questions / feedback.
Happy Programming
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub