简体   繁体   English

关于 Azure Cosmos DB 中的 CreateItemAsync 和 new PartitionKey 的困惑 - .NET Core

[英]Confusion about CreateItemAsync and new PartitionKey in Azure Cosmos DB - .NET Core

I am trying to setup a simple console project with Azure Cosmos DB.我正在尝试使用 Azure Cosmos DB 设置一个简单的控制台项目。 I am confused about the Partition Key Usage.我对分区键用法感到困惑。

Note, I have already looked at this solution, but I believe I have a slightly different situation.请注意,我已经看过这个解决方案,但我相信我的情况略有不同。

I am setting up the container like this.我正在像这样设置容器。

private string PartitionKeyPath = "/ProductPartitionKey";
this.container = await this.database.CreateContainerIfNotExistsAsync(containerId, PartitionKeyPath);

Here is an example JSON, as it gets stored in the container.这是一个示例 JSON,因为它存储在容器中。 So, I am successful in getting items added.所以,我成功地添加了项目。

{
    "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
}

I am able to do an insert with both the following lines of usage.我可以使用以下两种用法进行插入。

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);

The issue is, why wont the following work?问题是,为什么下面的工作不起作用?

var tempPartitionKey2 = new PartitionKey("/ProductPartitionKey");
ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct, tempPartitionKey2);

or this.或这个。

var tempPartitionKey3 = new PartitionKey("ProductPartitionKey");
ItemResponse<Product> tempProductResponse = await this.container.CreateItemAsync<Product>(tempProduct, tempPartitionKey3);

I get the same error in the linked stack question.我在链接堆栈问题中遇到了同样的错误。

PartitionKey extracted from document doesn't match the one specified in the header

so, ultimately, I am trying to understand, what string literal can I use, with new PartitionKey()?所以,最终,我试图理解,我可以使用 new PartitionKey() 使用什么字符串文字?

Or,或者,

is it so that, at current point of time, this is the only way to set and use Partition Key and I should not be trying to set the Partition Key with direct, string literal values?是不是这样,在当前时间点,这是设置和使用分区键的唯一方法,我不应该尝试使用直接的字符串文字值设置分区键? Perhaps, it is not a good way to do this, and azure cosmos library has stopped or removed that ability.或许,这不是一个好办法,蔚蓝宇宙图书馆已经停止或移除了那个能力。

var tempPartitionKey2 = new PartitionKey("/ProductPartitionKey");
ItemResponse < Product > tempProductResponse = await this.container.CreateItemAsync < Product > (tempProduct, tempPartitionKey2);

This does not work because you are not passing the Partition Key Value, you are passing the name of the property configured as Partition Key Path in the Container.这不起作用,因为您没有传递分区键值,而是传递配置为容器中分区键路径的属性的名称。

The error comes when the backend receives your item Json, and sees that the value does not correspond with that the item Json has.当后端收到您的项目 Json 并发现该值与项目 Json 所具有的值不对应时,就会出现错误。

When creating an item, the operation requires the item content, plus the value of the Partition Key Path for that document, the Partition Key Value.创建项目时,该操作需要项目内容,以及该文档的分区键路径值,即分区键值。 That is why this works:这就是为什么这有效:

var tempPartitionKey = new PartitionKey(tempProduct.ProductPartitionKey);

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

because you are passing the value of the property.因为您正在传递该属性的值。

If you don't specify the PartitionKey parameter, the SDK needs to extract it from the item payload, which takes a performance/CPU hit.如果您未指定 PartitionKey 参数,则 SDK 需要从项目负载中提取它,这会对性能/CPU 造成影响。 So it is recommended that, if you can, specify the value as the PartitionKey parameter.所以建议,如果可以,将值指定为 PartitionKey 参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM