January 14, 2023 by Anuraj Estimated read time : 8 mins
This post is about implementing CRUD operations with ASP.NET Core Web API and Mongo DB. In this post I am using a nuget package with the name MongoFramework - which is an Entity Framework like implementation for MongoDb. I am following the schema and implementation of the Microsoft Learn documentation about Create a web API with ASP.NET Core and MongoDB.
First I am creating a ASP.NET Core Web API project with Minimal API using the command dotnet new webapi --output BookStoreApi --use-minimal-apis. Next I am adding the nuget package - MongoFramework with the command - ` dotnet add package MongoFramework. Now we are ready with the setup. Next we will be creating a model POCO class Book` and here is the implementation.
Please note, the type of the Id property is string not an integer. Next we need to create the DbContext class, here is the implementation is little different from Entity Framework Core, instead of DbContext class, we should inherit from MongoDbContext class. And instead of DbSet we should use MongoDbSet class / type.
Unlike the DbContextOptions, the constructor accepts an instance of IMongoDbConnection - this is also a difference between EF Core and MongoFramework.
And the dependency injection code is also little different from EF Core. First you need to inject the IMongoDbConnection object and then inject the MongoDbContext object and then you will be able to access the MongoDbContext instance in all the minimal api methods or controllers. Here is the dependency injection code.
And in the method / api endpoints the BookStoreDbContext can be access like any other injected service. Here is the Get All Books implementation.
This way you will be able to implement all the CRUD operations with the BookStoreDbContext object. But for testability I am implementing a repository class - we got a similar implementation in the Microsoft Learn Documentation as well.
Here is the repository interface.
And here is the implementation.
Now we need to update the dependency injection code to inject repository service as well. Here is the modified code.
And I implemented the CRUD operations with Minimal API and MongoFramework here.
Make sure, you’re creating a connection string inside appsettings.json to your mongodb database. I am running with my local mongo installation. And navigate to /swagger endpoint you will be able to see a screen like this.
If you execute the /Books endpoint you will get a empty array. Let us add one or more books, and will see the results. I am using the same data from the learn page for testing purposes.
If you notice, Mongo is generating a unique Id and returning it in the created response.
This way you will be able to implement CRUD operations in ASP.NET Core Minimal API with Mongo DB. Source code for the implementation is available on GitHub. Here is a video which talks about different features in MongoFramework in Youtube and you can find the source code here - https://github.com/TurnerSoftware/MongoFramework