读取文档

已完成

若要对容器中的现有项执行点读取,需要两项内容。

首先,需要项的唯一 ID。 在这里,我们将该 ID 存储在一个同名的变量中。

string id = "027D0B9A-F9D9-4C96-8213-C8546C4AAE71";

其次,需要创建一个 PartitionKey 类型的变量,并含有要寻找的项在分区键路径上的字符串值。

string categoryId = "26C74104-40BC-4541-8EF5-9892F7F03D72";
PartitionKey partitionKey = new (categoryId);

准备好这两项内容后,就可以调用异步和泛型 ReadItemAsync<> 方法,在本例中,该方法将返回一个给定的泛型类型 Product 的项。

Product saddle = await container.ReadItemAsync<Product>(id, partitionKey);

此时,可以访问 saddle 变量的属性,并像其他局部变量一样将其打印到控制台。

string formattedName = $"New Product [${saddle}]";
Console.WriteLine(formattedName);