This post is about configuring Mongo DB and creating ASP.NET Core Web API to work with Mongo DB using
MongoDB.EntityFrameworkCore nuget package. Recently MongoDB team announced a EF Core provider for MongoDB. Earlier this year I blogged about MongoDB and ASP.NET Core using
MongoFramework nuget package. In this blog post we are using
MongoDB.EntityFrameworkCore nuget package which is from MongoDB.
Setting up MongoDB
For configuring MongoDB we can use Docker. First we can run the command -
docker run --name mongo-db --publish 27017:27017 -v mongovolume:/data/db -d mongo:latest - the
--name parameter helps us to identify the name of the container. The
--publish parameter helps us to expose the 27017 port, and
--volume parameter helps us to persist the data - it is optional. Once the Mongo DB running, we can use the connection string like this in ASP.NET Core -
Setting up the Web API
First we need to create a Web API, we can do this by running the command
dotnet new webapi --name BooksApi --output BooksApi --use-minimal-apis. Next we need to add reference of the
MongoDB.EntityFrameworkCore nuget package - we can do this with the command -
dotnet add .\BooksApi\ package MongoDB.EntityFrameworkCore --prerelease - since it is preview we need the
--prerelease parameter. Now we have completed the Web API setup. Next open the project using VS Code. We can create a
BookEntity class - which represents the MongoDB collection,
Book class which is a view model class. And we need to create and configure DbContext class.
BookEntity class for the
ObjectId is the type. Here is code for
Book view model class.
The Book view model class is similar to the BookEntity class except the Id is not part of the Book class. Next we can create the DbContext class.
I am using the primary constructor feature of .NET 8.0 / C# 12 for the database context. And finally we can configure DbContext as part of dependency injection.
I am using the simple overload - there are other overloads are available as well - which accepts the MongoClient instance and all.
Now we are ready to implement the CRUD methods.
Web API implementation
Like any other dependency we will be able to get the instance of DbContext in the minimal API methods like this.
Like this we will be able to implement all the CRUD methods. Full code implementation is available on GitHub. This nuget package is still in preview. If you encounter any issues / bugs report them. Here is the Youtube video which explains about the NuGet package and how to use it.