Azure Cosmos DB libraries for .NET

Overview

Azure Cosmos DB is a globally distributed, multi-model database service. It is designed to elastically and independently scale throughput and storage across any number of geographical regions with a comprehensive SLA. With Azure Cosmos DB, you can store and access document, key-value, wide-column, and graph databases by using APIs and programming models.

Get started with Azure Cosmos DB.

Client library

Use the Azure Cosmos DB .NET client library to access and store data in an existing Azure Cosmos DB data store. To automate creation of a new Azure Cosmos DB account, use the Azure portal, CLI, or PowerShell.

Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

To install version 3.x, which targets .NET standard:

Visual Studio Package Manager

Install-Package Microsoft.Azure.Cosmos

.NET Core CLI

dotnet add package Microsoft.Azure.Cosmos

Code Example

This example connects to an existing Azure Cosmos DB SQL API database, creates a new database and container, reads an item from the container, and deserializes it to a TodoItem object. This example uses version 3.x of the .NET SDK.

// CosmosClient should always be a singleton for an application
using (CosmosClient cosmosClient = new CosmosClient("endpoint", "primaryKey"))
{
    Container container = cosmosClient.GetContainer("DatabaseId", "ContainerId");
    // Read item from container
    CosmosItemResponse<TodoItem> todoItemResponse = await container.ReadItemAsync<TodoItem>("ItemId", new PartitionKey("partitionKeyValue"));
}

Samples