SQL API 的 Azure Cosmos DB.NET V3 SDK (Microsoft.Azure.Cosmos) 示例Azure Cosmos DB.NET V3 SDK (Microsoft.Azure.Cosmos) examples for the SQL API

azure-cosmos-dotnet-v3 GitHub 存储库中包含可对 Azure Cosmos DB 资源执行 CRUD 操作和其他常见操作的最新 .NET 示例解决方案。The azure-cosmos-dotnet-v3 GitHub repository includes the latest .NET sample solutions to perform CRUD and other common operations on Azure Cosmos DB resources. 如果你熟悉旧版 .NET SDK,则可能习惯使用术语“集合”和“文档”。If you're familiar with the previous version of the .NET SDK, you may be used to the terms collection and document. 由于 Azure Cosmos DB 支持多个 API 模型,因此 3.0 版的 .NET SDK 使用通用术语“容器”和“项”。Because Azure Cosmos DB supports multiple API models, version 3.0 of the .NET SDK uses the generic terms "container" and "item". 容器可以是集合、图或表。A container can be a collection, graph, or table. 项可以是文档、边缘/顶点或行,是容器中的内容。An item can be a document, edge/vertex, or row, and is the content inside a container. 本文提供:This article provides:

  • 指向每个示例 C# 项目文件中各项任务的链接。Links to the tasks in each of the example C# project files.
  • 指向相关的 API 参考内容的链接。Links to the related API reference content.

先决条件Prerequisites

已安装包含 Azure 开发工作流的 Visual Studio 2019Visual Studio 2019 with the Azure development workflow installed

Azure 订阅,或免费的 Cosmos DB 试用帐户An Azure subscription or free Cosmos DB trial account

备注

这些示例都是独立的,运行完成后,可对其进行设置和清理。The samples are self-contained, and set up and clean up after themselves. 每次执行此操作时,都会根据容器的性能层,对订阅收取使用 1 小时的费用。Each occurrence bills your subscription for one hour of usage in your container's performance tier.

数据库示例Database examples

DatabaseManagement 项目示例的 RunDatabaseDemo 方法演示如何执行以下任务。The RunDatabaseDemo method of the sample DatabaseManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 数据库,请参阅使用数据库、容器和项To learn about Azure Cosmos databases before you run the following samples, see Work with databases, containers, and items.

任务Task API 参考API reference
创建数据库Create a database CosmosClient.CreateDatabaseIfNotExistsAsyncCosmosClient.CreateDatabaseIfNotExistsAsync
按 ID 读取数据库Read a database by ID Database.ReadAsyncDatabase.ReadAsync
读取帐户的所有数据库Read all the databases for an account CosmosClient.GetDatabaseQueryIteratorCosmosClient.GetDatabaseQueryIterator
删除数据库Delete a database Database.DeleteAsyncDatabase.DeleteAsync

容器示例Container examples

ContainerManagement 项目示例的 RunContainerDemo 方法演示如何执行以下任务。The RunContainerDemo method of the sample ContainerManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 容器,请参阅使用数据库、容器和项To learn about Azure Cosmos containers before you run the following samples, see Work with databases, containers, and items.

任务Task API 参考API reference
创建容器Create a container Database.CreateContainerIfNotExistsAsyncDatabase.CreateContainerIfNotExistsAsync
使用自定义索引策略创建容器Create a container with custom index policy Database.CreateContainerIfNotExistsAsyncDatabase.CreateContainerIfNotExistsAsync
更改容器的已配置性能Change configured performance of a container Container.ReplaceThroughputAsyncContainer.ReplaceThroughputAsync
按 ID 获取容器Get a container by ID Container.ReadContainerAsyncContainer.ReadContainerAsync
读取数据库中的所有容器Read all the containers in a database Database.GetContainerQueryIteratorDatabase.GetContainerQueryIterator
删除容器Delete a container Container.DeleteContainerAsyncContainer.DeleteContainerAsync

项示例Item examples

ItemManagement 项目示例的 RunItemsDemo 方法演示如何执行以下任务。The RunItemsDemo method of the sample ItemManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 项,请参阅使用数据库、容器和项To learn about Azure Cosmos items before you run the following samples, see Work with databases, containers, and items.

任务Task API 参考API reference
创建项Create an item Container.CreateItemAsyncContainer.CreateItemAsync
按 ID 读取项Read an item by ID container.ReadItemAsynccontainer.ReadItemAsync
查询项Query for items container.GetItemQueryIteratorcontainer.GetItemQueryIterator
替换项Replace an item container.ReplaceItemAsynccontainer.ReplaceItemAsync
更新插入项Upsert an item container.UpsertItemAsynccontainer.UpsertItemAsync
删除项Delete an item container.DeleteItemAsynccontainer.DeleteItemAsync
使用条件 ETag 检查替换项Replace an item with conditional ETag check RequestOptions.IfMatchEtagRequestOptions.IfMatchEtag

索引示例Indexing examples

IndexManagement 项目示例的 RunIndexDemo 方法演示如何执行以下任务。The RunIndexDemo method of the sample IndexManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的索引,请参阅索引策略索引类型索引路径To learn about indexing in Azure Cosmos DB before you run the following samples, see index policies, index types, and index paths.

任务Task API 参考API reference
从索引中排除项Exclude an item from the index IndexingDirective.ExcludeIndexingDirective.Exclude
使用延迟索引Use Lazy indexing IndexingPolicy.IndexingModeIndexingPolicy.IndexingMode
从索引中排除指定的项路径Exclude specified item paths from the index IndexingPolicy.ExcludedPathsIndexingPolicy.ExcludedPaths

查询示例Query examples

示例 Queries 项目的 RunDemoAsync 方法显示了如何使用 SQL 查询语法、查询的 LINQ 提供程序和 Lambda 执行以下任务。The RunDemoAsync method of the sample Queries project shows how to do the following tasks using the SQL query grammar, the LINQ provider with query, and Lambda. 若要在运行以下示例之前了解 Azure Cosmos DB 中的 SQL 查询引用,请参阅 Azure Cosmos DB 的 SQL 查询示例To learn about the SQL query reference in Azure Cosmos DB before you run the following samples, see SQL query examples for Azure Cosmos DB.

任务Task API 参考API reference
查询单个分区中的项Query items from single partition container.GetItemQueryIteratorcontainer.GetItemQueryIterator
查询多个分区中的项Query items from multiple partitions container.GetItemQueryIteratorcontainer.GetItemQueryIterator
使用 SQL 语句进行查询Query using a SQL statement container.GetItemQueryIteratorcontainer.GetItemQueryIterator

更改源示例Change feed examples

示例 ChangeFeed 项目的 RunBasicChangeFeed 方法演示如何执行以下任务。The RunBasicChangeFeed method of the sample ChangeFeed project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的更改源,请参阅读取 Azure Cosmos DB 更改源更改源处理器To learn about change feed in Azure Cosmos DB before you run the following samples, see Read Azure Cosmos DB change feed and Change feed processor.

任务Task API 参考API reference
基本的更改源功能Basic change feed functionality Container.GetChangeFeedProcessorBuilderContainer.GetChangeFeedProcessorBuilder
读取特定时间的更改源Read change feed from a specific time Container.GetChangeFeedProcessorBuilderContainer.GetChangeFeedProcessorBuilder
从头读取更改源Read change feed from the beginning ChangeFeedProcessorBuilder.WithStartTime(DateTime)ChangeFeedProcessorBuilder.WithStartTime(DateTime)
从更改源处理器迁移到 V3 SDK 中的更改源MIgrate from change feed processor to change feed in V3 SDK Container.GetChangeFeedProcessorBuilderContainer.GetChangeFeedProcessorBuilder

服务器端编程示例Server-side programming examples

示例 ServerSideScripts 项目的 RunDemoAsync 方法演示如何执行以下任务。The RunDemoAsync method of the sample ServerSideScripts project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的服务器端编程,请参阅存储过程、触发器和用户定义的函数To learn about server-side programming in Azure Cosmos DB before you run the following samples, see Stored procedures, triggers, and user-defined functions.

任务Task API 参考API reference
创建存储过程Create a stored procedure Scripts.CreateStoredProcedureAsyncScripts.CreateStoredProcedureAsync
执行存储过程Execute a stored procedure Scripts.ExecuteStoredProcedureAsyncScripts.ExecuteStoredProcedureAsync
删除存储过程Delete a stored procedure Scripts.DeleteStoredProcedureAsyncScripts.DeleteStoredProcedureAsync