Getting started with .NET Core API, MongoDB, and Transactions

Alex Alves
6 min readAug 9, 2020

MongoDB is a kind of NoSQL database. NoSQL is a document-oriented database that is organized as a JSON. Some points about MongoDB:

  • Performance: It stores a majority of data in RAM, so the query performance is much better here than in a relational database. But it requires more RAM and precise indexes.
  • Simplicity: Some users consider the query syntax is simpler here than in relational databases. The installation, configuration, and execution are simple and quick to do. And the learning curve is shorter than in the others.
  • Flexibility: It’s dynamic because it doesn’t have a predefined schema.
  • Scalability: It uses shards for horizontal scalability, which makes it easier to increase storage capacity.
  • Transaction: v3.6 and beyond allow using the transaction concept. And v4.0 and beyond allow using the multi-document transaction concept.

Now that you know a little more about MongoDB, let’s go to our goal! This article proposes to teach you how you can build a .NET Core API connecting with MongoDB and use the Transactions with Dependency Injection.


For this example, don’t consider the structure and the architecture, it was built in this way just because I think that it’s easier to explain. So, I won’t explain the layers, folders, and other things in detail, except those needed by the use of MongoDB and the transactions.

The project

In this case, I used the Visual Studio 2019, Community version. So, with the VS installed, we select the “ASP.NET Core Web Application” option and select the API type.

About the base structure

After creating the project, we create five folders like below:

  • Controllers: responsible to answer the requests
Alex Alves

Bachelor in Computer Science, MBA in Software Architecture and .NET Developer.