
This Azure tutorial will discuss CRUD Operations In Azure Cosmos DB C# or how to perform CRUD operations in Azure Cosmos DB. Along with this, we will also discuss the below topics.
CRUD Operations In Azure Cosmos DB C#
If you are very new to Azure Cosmos DB, then you are at the right place where you will learn the CRUD operations in Azure Cosmos DB using C#.
Before starting the CRUD operation in Azure Cosmos DB, let’s discuss the prerequisites needed here.
Prerequisites
Below are the prerequisites to perform the Azure Cosmos DB’s CRUD operation.
- You must have an Azure Account or Azure Subscription. If you don’t have an Azure account, you can create a free Azure account now.
- You must create an Azure Cosmos DB account.
- On your Dev machine, you must install Visual Studio 2019.
Let’s start with the actual functionality if you are ready with all the prerequisites.
The first step is to create a console app using Visual Studio 2019.
Creating the Console Application
Follow the below steps to create the console app
- Open the Visual Studio 2019 on your dev machine.
- Click on the Create a new Project button.
- Search for the template “Console App (.Net Framework).” Choose the template and click on the Next button.
- On the Configure your new project window, provide the below details
- Project name: Provide a name for your project.
- Location: Choose a location where you want to save your project.
- Framework: Choose the latest .Net framework.
Finally, click on the Create button to create the console application.

Now, the project has been created successfully. As the next step, we must add the required NuGet package to the solution.
Adding the NuGet package
- To add the Nuget package, Right-click on the project and select the Manage NuGet Packages option.

- Under the Browse tab, search for ‘documentdb’.
- Click on the search result in Microsoft.Azure.DocumentDB and click on the Install button, as highlighted below.

- Now. click on the I Accept button to start the installation. It will take a few seconds to finish the installation.
As the next step, let’s do the App. config file changes. Add the code below to the App. config file
<appSettings>
<!--This is your Azure Cosmos DB Endpoint URL-->
<add key="Endpoint" value =""/>
<!--This is your Azure Cosmos DB Primary Key-->
<add key="PrimaryKey" value =""/>
</appSettings>To get the values of the Endpoint and the Primary key of the Azure Cosmos DB, you need to follow the below instructions.
- Log in to the Azure Portal and navigate to your Azure Cosmos DB account.
- On the Azure Cosmos DB account page, click the Keys option from the left navigation.
- Copy the URI value, which is nothing but the value for the Endpoint key that we have mentioned on the App. Config file.
- Copy the Primarykey value, which is nothing but the value for the Primarykey we have mentioned on the App. config file.

The complete App. config file will be like the below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<!--This is your Azure Cosmos DB Endpoint URL-->
<add key="Endpoint" value ="https://mydemocdb38.documents.azure.com:xxx/"/>
<!--This is your Azure Cosmos DB Primary Key-->
<add key="PrimaryKey" value ="YM5doVlb8lnNt6Qfc7zAdNxxxxxxxxxxxxxxxxxxxxxxxxNMmA623ot6csj71RwrVuh0YHhuHCo5eWq3DekhNiw=="/>
</appSettings>
</configuration>Note: Make sure to update the value for the Endpoint URI and Primary key based on yours.
Now, we need to make the code changes for the Program.cs file. The first step is, we need to initialize the document client instance.