Unleashing the Power of Cosmos DB: A Step-by-Step Guide to Setting up a Connection in .Net with Code Examples

Mark Anderson
3 min readJan 13, 2023

Cosmos DB is a globally distributed, multi-model database service provided by Microsoft Azure. It allows you to store, retrieve, and manage data in a highly-scalable and performant manner. In this blog post, we will walk through the process of setting up a Cosmos DB connection in .Net using code examples.

Before we begin, it’s important to note that there are two types of Cosmos DB connections: a SQL API connection and a MongoDB API connection. In this post, we will focus on setting up a SQL API connection.

Step 1: Create a Cosmos DB account

The first step in setting up a Cosmos DB connection is to create a Cosmos DB account. To do this, navigate to the Azure portal and click on the “Create a resource” button. In the search bar, type “Cosmos DB” and select the option that appears.

Once you have selected Cosmos DB, you will be prompted to fill in some information about the account you are creating. This includes the subscription, resource group, and account name. You will also need to choose a region for the account.

Step 2: Create a database and collection

Once your Cosmos DB account is created, you will need to create a database and collection. You can do this by navigating to the Cosmos DB account in the Azure portal and selecting the “Data Explorer” tab. From there, click on the “New Container” button and fill in the necessary information about the database and collection.

Step 3: Get the connection string

To establish a connection to your Cosmos DB account, you will need the connection string. You can find this by navigating to the Cosmos DB account in the Azure portal and selecting the “Keys” tab. From there, you will see the primary and secondary connection strings.

Step 4: Install the necessary NuGet packages

To work with Cosmos DB in .Net, you will need to install the following NuGet packages:

  • Microsoft.Azure.Cosmos
  • Microsoft.Azure.Cosmos.Client

You can install these packages by opening the NuGet package manager in Visual Studio and searching for them.

Step 5: Add the code to establish a connection

Once you have the connection string and the necessary NuGet packages installed, you can add the code to establish a connection to your Cosmos DB account. Here is an example of how to do this:

using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Client;

// Replace with your Cosmos DB connection string
string connectionString = "YOUR CONNECTION STRING HERE";

// Create a Cosmos DB client
CosmosClient cosmosClient = new CosmosClient(connectionString);

// Create a reference to the database and collection
Database database = cosmosClient.GetDatabase("YOUR DATABASE NAME HERE");
Container container = database.GetContainer("YOUR CONTAINER NAME HERE");

In the above example, we first create a CosmosClient object by passing in the connection string. We then use this client to get a reference to the database and collection that we created earlier.

Step 6: Perform CRUD operations

Now that you have a connection to your Cosmos DB account, you can perform CRUD (create, read, update, and delete) operations on the data in the database. Here are some examples of how to do this:

// Create a new item
dynamic item = new { id = "1", name = "John Doe" };
ItemResponse<dynamic> itemResponse = await container.CreateItemAsync(item, new PartitionKey("1")).ConfigureAwait(false);

// Read an item
ItemResponse<dynamic> readResponse = await container.ReadItemAsync<dynamic>("1", new PartitionKey("1")).ConfigureAwait(false);

// Update an item
dynamic updateItem = new { id = "1", name = "Jane Doe" };
ItemResponse<dynamic> updateResponse = await container.UpsertItemAsync<dynamic>(updateItem, new PartitionKey("1")).ConfigureAwait(false);

// Delete an item
ItemResponse<dynamic> deleteResponse = await container.DeleteItemAsync<dynamic>("1", new PartitionKey("1")).ConfigureAwait(false);

In the above examples, we first create a new item by passing in the item data and a partition key. Then, we read the item using its id and partition key. Next, we update the item by passing in the updated data and partition key. Lastly, we delete the item using its id and partition key. It’s worth noting that the above examples use dynamic types, but you can also use a strongly typed class as the item data if desired.

Conclusion

Setting up a Cosmos DB connection in .Net is a straightforward process that can be accomplished with a few simple steps. By following the steps outlined in this blog post, you can easily establish a connection to your Cosmos DB account and perform CRUD operations on the data in the database. With the power and scalability of Cosmos DB at your disposal, you can build robust and performant applications that can handle large amounts of data with ease.

--

--

Mark Anderson
0 Followers

A seasoned software developer with over 12 years of experience. Sharing his knowledge through writing and speaking at meetups and conferences.