You have probably heard of MongoDB, one of the most popular NoSQL databases today. In this article, we are going to create an application in .NET 6 and connect it to MongoDB Atlas simply and objectively.
Something that is strictly linked to software development is databases, so software developers must be familiar with this subject. A database that has been standing out in recent years is MongoDB, which is a NoSQL database and has a valuable resource, the MongoDB Atlas.
In this article, we’ll have an introduction to MongoDB Atlas, we’ll build a .NET 6 application from scratch, and we’ll connect it to the database in the Atlas cluster.
You can access the complete source code of the project at this link: Source Code.
The application used in the example will be a minimal API available in .NET 6. So, you need to have the .NET 6 SDK installed. Link to download: .NET 6 SDK.
To create the application, use the command below:
dotnet new web -o StudentManager
Open the project with your favorite IDE. In this example, I will use Visual Studio 2022.
Double-click on the project (StudentManager.csproj) and add the following code to install the dependencies, next recompile the project.
“Global usings” are features available in C# 10 and they make it possible to use references for any class in the project.
To use this feature, create a folder called “Helpers” and inside it, create a class called “GlobalUsings.” Replace the generated code with the code below. Don’t worry about import errors—later we’ll create the namespaces that don’t exist yet.
Next, let’s create our database entity. So, create a folder called “Models” and inside it, add a class called “Student” and paste the following code into it:
MongoDB Atlas is one of the most popular non-relational databases today. As a database service, it provides all of the features of MongoDB, in addition to automating database administration tasks such as database configuration, infrastructure provisioning, patches, scaling events, backups and more.
After creating the example application in .NET and implementing the database entity, we will configure MongoDB Atlas. If you are new to MongoDB Atlas, I recommend this guide for creating and configuring your first cluster: MongoDB Atlas Getting Started.
With the cluster configured, we can create a database “student_db” and a collection “students” as in the image below:
To connect our application with the cluster and have access to the created database, we need to obtain the connection string, which we will use later on. To get it, just follow the steps as shown in the images below:
On your database, click “Connect.”
Select “Connect your application.”
Select driver C#/.NET version 2.13 or later, and you’ll get your connection string.
Now that we have the connection to the cluster, let’s implement the configuration in the project.
So, replace the code from the “appsettings.json” file with the code below:
Important! In your cluster connection, replace “<password>” with the password for your cluster user, and replace “myFirstDatabase” with “student_db”. In the code above, replace “<your cluster connection>” with your cluster connection.
Now, let’s create the class that will contain the variables responsible for storing the values of the connection string created earlier.
So, create a folder called “Data,” and inside it create a new class called “StudentDatabaseSettings” and put the following code in it:
Now, let’s create a “service” class that will be responsible for creating a database connection and performing CRUD operations.
So, create a new folder called “Services” and inside it, create a new class called “StudentService” and put the code below inside it:
Now let’s create the settings to enable the swagger and add the endpoints that will use the Service methods created earlier, plus the dependency injection of the “StudentService” class and the configuration that will fill the values of variables of the database.
In the Program.cs file, replace the existing code with the code below:
If you followed the previous steps, our application is ready to run and test. For the Swagger page to be displayed when starting the application, you will need to add the configuration as shown in the image below:
After starting the application, we can do CRUD operations. In this tutorial, I’m using Fiddler Everywhere.
Below is the entry in the MongoDB Atlas database:
In this article, we created a .NET 6 application that performs CRUD operations and uses the MongoDB Atlas as its database.
The advantages of using MongoDB Atlas are many and the union with .NET makes it an even more amazing tool. Feel free to suggest improvements—your feedback is always welcome!