你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
用于 .NET 的 Azure Cosmos DB 库
概述
Azure Cosmos DB 是一种全球分布式多模型数据库服务。 它旨在通过一个综合性 SLA 跨任意数量的地理区域以富有弹性的方式独立缩放吞吐量和存储空间。 有了 Azure Cosmos DB,可以通过使用 API 和编程模型存储和访问文档、键值、宽列和图形数据库。
Azure Cosmos DB 入门。
客户端库
使用 Azure Cosmos DB .NET 客户端库在现有 Azure Cosmos DB 数据存储中访问和存储数据。 若要自动创建新的 Azure Cosmos DB 帐户,请使用 Azure 门户、CLI 或 PowerShell。
直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包。
安装面向 .NET Standard 的 3.x 版:
Visual Studio 包管理器
Install-Package Microsoft.Azure.Cosmos
.NET Core CLI
dotnet add package Microsoft.Azure.Cosmos
代码示例
此示例连接到现有 Azure Cosmos DB SQL API 数据库、创建新数据库和容器、从容器中读取某个项以及将其反序列化为 TodoItem
对象。 此示例使用 .NET SDK 的 3.x 版本。
// 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"));
}