您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .NET 的 Azure Cosmos DB 库Azure Cosmos DB libraries for .NET

概述Overview

Azure Cosmos DB 是一种全球分布式多模型数据库服务。Azure Cosmos DB is a globally distributed, multi-model database service. 它旨在通过一个综合性 SLA 跨任意数量的地理区域以富有弹性的方式独立缩放吞吐量和存储空间。It is designed to elastically and independently scale throughput and storage across any number of geographical regions with a comprehensive SLA. 有了 Azure Cosmos DB,可以通过使用 API 和编程模型存储和访问文档、键值、宽列和图形数据库。With Azure Cosmos DB, you can store and access document, key-value, wide-column, and graph databases by using APIs and programming models.

Cosmos DB 入门Get started with Cosmos DB.

客户端库Client library

使用 Azure Cosmos DB .NET 客户端库在现有 Azure Cosmos DB 数据存储中访问和存储数据。Use the Azure Cosmos DB .NET client library to access and store data in an existing Azure Cosmos DB data store. 若要自动创建新的 Azure Cosmos DB 帐户,请使用 Azure 门户、CLI 或 PowerShell。To automate creation of a new Azure Cosmos DB account, use the Azure portal, CLI, or PowerShell.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

若要安装版本 2.x,请执行以下命令:To install version 2.x:

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.DocumentDB.Core

.NET Core CLI.NET Core CLI

dotnet add package Microsoft.Azure.DocumentDB.Core

若要安装 3.0 预览版(面向 .NET Standard),请执行以下命令:To install the preview of version 3.0, which targets .NET standard:

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.Cosmos -prerelease

.NET Core CLI.NET Core CLI

dotnet add package Microsoft.Azure.Cosmos

代码示例Code Example

此示例连接到现有的Azure Cosmos DB SQL API 数据库,从集合中读取文档,并将文档反序列化为 TodoItem 对象。This example connects to an existing Azure Cosmos DB SQL API database, reads a document from a collection, and deserializes it as an TodoItem object. 此示例使用 .NET SDK 的 2.x 版本。This example uses version 2.x of the .NET SDK.

/* Include this "using" directive...
using Microsoft.Azure.Documents.Client;
*/

DocumentClient client = new DocumentClient(endpointUri, authKeyString);
Uri documentUri = UriFactory.CreateDocumentUri("MyDatabaseName", "MyCollectionName", "DocumentId");
var todoItem = client.ReadDocumentAsync<TodoItem>(documentUri);

此示例连接到现有 Azure Cosmos DB SQL API 数据库、创建新数据库和容器、从容器中读取某个项以及将其反序列化为 TodoItem 对象。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. 此示例使用 .NET SDK 的 3.x 版本。This example uses version 3.x of the .NET SDK.

using (CosmosClient cosmosClient = new CosmosClient("endpoint", "primaryKey"))
{
    // Read item from container
    CosmosItemResponse<TodoItem> todoItemResponse = await cosmosClient.Databases["DatabaseId"].Containers["ContainerId"].Items.ReadItemAsync<TodoItem>("partitionKeyValue", "ItemId");
}

示例Samples

查看 Azure Cosmos DB 示例的完整列表View the complete list of Azure Cosmos DB samples.