Step 1: Set up Prerequisites
We need to set up one of the following required pre-requisites to perform the CRUD operation in the Azure Cosmos DB with C#.
- Azure Subscription OR
- Azure Cosmos DB Emulator
I hope you have completed the required setup as explained in this article, including the creation of the Azure Cosmos account and database.
Now let's start creating the ASP.NET Core web API project with C# to create the CRUD operations.
Step 1: Create ASP.NET Core web api Project with C#
- Start then All Programs and select "Microsoft Visual Studio".
- Once the Visual Studio Opens, Then click on Continue Without Code.
- Then Go to Visual Studio Menu, click on File => New Project then choose ASP.NET Core Web Api Project Template.
- Then define the project name, location of the project, then click on the next button.
- On the next screen, provide the additional details: framework, authentication type, and check the Enable Open API Support checkbox as shown below.
The preceding steps will create the ASP.NET Core Web API application and solution explorer. It will look like what is shown in the following image.
Step 2: Add Microsoft.Azure.Cosmos Nuget Package ReferenceThe Microsoft.Azure.Cosmos is the the latest nuget package to interact with the Azure cosmos DB. The Microsoft.Azure.Cosmos supports the basic to custom and complex database operations, follow the following steps to add the Nuget package.
- Right click on the Solution Explorer, find Manage NuGet Package Manager and click on it
- After as shown into the image and type in search box Microsoft.Azure.Cosmos
- Select Microsoft.Azure.Cosmos as shown into the image,
- Choose version of Microsoft.Azure.Cosmos library and click on install button
Step 3:Create the Model Class
- First, delete the default model class, which is created outside the folder structure, so we can start from scratch.
- Next, create the folder named Model by right clicking on the solution explorer.
- Create the model class Employee Model by right clicking on the Model folder, as shown in the following image
Step 4: Add the ControllerCreate the Empty API Controller class EmployeeController by right clicking on the Controller folder as shown in the following image.
After adding the model class and API controller class, the solution explorer will look like the following:
- Define the following route at controller level, so that we can add the multiple Get, Post, Put, or Delete and avoid the name ambiguity exception.
- Declare the following variable and set the Azure Cosmos DB configuration by copying the details from step 2.
Step 5: Create Method to Add Employee
As explained in step 2, we are passing the ID and Department as a partition key as part of the employee input payload while adding the employee details. These parameters must be part of the input parameter payload.
Step 6: Create Method to Get Employees
Step 7: Create Method to Get Employee by ID
Step 8: Create Method to Update Employee
Step 9: Create Method to Delete Employee
Now, we have all the code and required configuration to work with Azure CosmosDB in our sample application.
Step 10: Run the ASP.NET Core API ApplicationNow press Keyboard F5 or the Visual Studio Run button to run the application. After running the application, the following screen will be shown in the browser with swagger UI having all the methods which we have created in our ASP.NET core web API, as shown in the following screenshot:
Step 11: Azure Cosmos DB CRUD Demo
Get List of Employees and Employee by Id
Update the Employee
Delete the Employee by Id
- Creating Azure Cosmos DB Using Azure Portal
- Now Azure Cosmos DB Free Tier Supports 25 GB Storage and 1000 Ru/s
- Now Azure Cosmos DB Support Partial Document Update
- Bind DropDownList Using ViewBag In ASP.NET Core
- How to Create ASP.NET Core Web Application
- Running ASP.NET Core Web Application in Docker Container
- How To Deploy ASP.NET Core Application On Docker Linux Container