Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure Cosmos DB-.NET内核中CreateItemAsync和new PartitionKey的混淆_Azure_Azure Cosmosdb - Fatal编程技术网

Azure Cosmos DB-.NET内核中CreateItemAsync和new PartitionKey的混淆

Azure Cosmos DB-.NET内核中CreateItemAsync和new PartitionKey的混淆,azure,azure-cosmosdb,Azure,Azure Cosmosdb,我正在尝试用Azure Cosmos DB设置一个简单的控制台项目。我对分区键的用法感到困惑 注意,我已经看过这个解决方案,但我相信我有一个稍微不同的情况 我正在这样设置容器 private string PartitionKeyPath = "/ProductPartitionKey"; this.container = await this.database.CreateContainerIfNotExistsAsync(containerId, Partition

我正在尝试用Azure Cosmos DB设置一个简单的控制台项目。我对分区键的用法感到困惑

注意,我已经看过这个解决方案,但我相信我有一个稍微不同的情况

我正在这样设置容器

private string PartitionKeyPath = "/ProductPartitionKey";
this.container = await this.database.CreateContainerIfNotExistsAsync(containerId, PartitionKeyPath);
下面是一个JSON示例,它存储在容器中。因此,我成功地添加了项目

{
    "Name": "Super Curtain",
    "UnitPrice": 500,
    "id": "124BBC08-F51C-4ED4-B961-8DD0C966F66F",
    "ProductPartitionKey": "Hello",
    "_rid": "m2VTANekt8ACAAAAAAAAAA==",
    "_self": "dbs/m2VTAA==/colls/m2VTANekt8A=/docs/m2VTANekt8ACAAAAAAAAAA==/",
    "_etag": "\"0500753f-0000-1800-0000-5f735ac70000\"",
    "_attachments": "attachments/",
    "_ts": 1601395399
}
我可以使用以下两种用法进行插入

var tempPartitionKey = new PartitionKey(tempProduct.ProductPartitionKey);

ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct, tempPartitionKey);
ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct);
所以,最终,我试图理解,对于新的PartitionKey(),我可以使用什么字符串文字

或者

在当前时间点上,这是设置和使用分区键的唯一方法,我不应该尝试使用直接的字符串文本值来设置分区键吗?也许,这不是一个好方法,azure cosmos库已经停止或删除了该功能; ItemResponsetempProductResponse=等待this.container.CreateItemAsync(tempProduct,tempPartitionKey2); 这不起作用,因为您没有传递分区键值,而是传递容器中配置为分区键值路径的属性的名称

当后端接收到您的项目Json,并且发现该值与项目Json的值不一致时,就会出现错误

创建项时,操作需要项内容加上该文档的分区键路径值,即分区键值。这就是为什么这样做的原因:

var tempPartitionKey = new PartitionKey(tempProduct.ProductPartitionKey);

ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct, tempPartitionKey);
var tempPartitionKey=new PartitionKey(tempProduct.ProductPartitionKey);
ItemResponsetempProductResponse=等待this.container.CreateItemAsync(tempProduct,tempPartitionKey);
因为您正在传递属性的值

如果不指定PartitionKey参数,SDK需要从项目负载中提取该参数,这会影响性能/CPU。因此,如果可以,建议将该值指定为PartitionKey参数。

var tempPartitionKey2=new PartitionKey(“/ProductPartitionKey”);
ItemResponsetempProductResponse=等待this.container.CreateItemAsync(tempProduct,tempPartitionKey2);
这不起作用,因为您没有传递分区键值,而是传递容器中配置为分区键值路径的属性的名称

当后端接收到您的项目Json,并且发现该值与项目Json的值不一致时,就会出现错误

创建项时,操作需要项内容加上该文档的分区键路径值,即分区键值。这就是为什么这样做的原因:

var tempPartitionKey = new PartitionKey(tempProduct.ProductPartitionKey);

ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct, tempPartitionKey);
var tempPartitionKey=new PartitionKey(tempProduct.ProductPartitionKey);
ItemResponsetempProductResponse=等待this.container.CreateItemAsync(tempProduct,tempPartitionKey);
因为您正在传递属性的值


如果不指定PartitionKey参数,SDK需要从项目负载中提取该参数,这会影响性能/CPU。因此,如果可以,建议将该值指定为PartitionKey参数。

ah。我明白了,现在,这比官方文件更清楚了。我传递的是名称,而不是键值。很好。有关于使用partitionKey参数的性能优势的文档吗?MS docs for Container.CreateItemAsync列出了partitionKey参数,但它只是说:
partitionKey用于该项。如果未指定,将通过从{T}
ah中提取来填充。我明白了,现在,这比官方文件更清楚了。我传递的是名称,而不是键值。很好。有关于使用partitionKey参数的性能优势的文档吗?MS docs for Container.CreateItemAsync列出了partitionKey参数,但它只是说:
partitionKey用于该项。如果未指定,将通过从{T}中提取来填充
PartitionKey extracted from document doesn't match the one specified in the header
var tempPartitionKey2 = new PartitionKey("/ProductPartitionKey");
ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct, tempPartitionKey2);
var tempPartitionKey = new PartitionKey(tempProduct.ProductPartitionKey);

ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct, tempPartitionKey);