从文档中提取的PartitionKey与CreateItemAsync的标头中指定的键不匹配 [英] PartitionKey extracted from document doesn't match the one specified in the header on CreateItemAsync

查看:108
本文介绍了从文档中提取的PartitionKey与CreateItemAsync的标头中指定的键不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Microsoft.Azure.Cosmos版本3.2.0时,我有点问题

I have a bit of a problem using Microsoft.Azure.Cosmos version 3.2.0,

跑步时

await this.Container.CreateItemAsync<LogEntity>(logEntity, new PartitionKey("anythingIPutHere"));

它抛出

Microsoft.Azure.Cosmos.CosmosException HResult = 0x80131500
消息=响应状态代码不指示成功:400子状态: 1001原因:(消息:{错误":["从 文档与标题中指定的文档不匹配]}

Microsoft.Azure.Cosmos.CosmosException HResult=0x80131500
Message=Response status code does not indicate success: 400 Substatus: 1001 Reason: (Message: {"Errors":["PartitionKey extracted from document doesn't match the one specified in the header"]}

但如果我愿意,

await this.Container.CreateItemAsync<LogEntity>(logEntity, new PartitionKey(logEntity.Id));

它有效,这是唯一可行的情况.

it works and it is the only case when it works.

我也尝试过 *将分区键的值作为属性放在对象中; *即使指定了partitionKey JSON属性名称,但没有成功;

I have also tried * Putting the value for the partition key as a property in the object; * Even specifying a partitionKey JSON property name but no success;

我查看了Microsoft网站上的一些指南,看来您可以将分区键指定为一些字符串,而不必指定为id或在对象上使用属性名称指定;那为什么会这样呢?

I looked over some guides on the Microsoft site and it seems you can specify the partition key to be some string, not necessary to be the id or specified with a property name on the object; so why is this happening?

推荐答案

我在创建容器时忽略了

this.Container = await this.Database.CreateContainerIfNotExistsAsync("testContainer", "/id");

我已将partitionKeyPath指定为/id的蜜蜂.

似乎分区键也必须驻留在对象上,并且必须具有partitionKeyPath属性名称的json属性,不带/像这样:

It also seems that the partition key must reside on the object and must have a json property of partitionKeyPath property name without / like:

[JsonProperty(PropertyName = "partition")]
public string Partition { get; set; }

如果partitionKeyPath/partition

this.Container = await this.Database.CreateContainerIfNotExistsAsync("testContainer", "/partition");

很抱歉,如果这很明显,我刚刚开始使用CosmoDb.

Sorry if this is obvious, I have just started playing around with CosmoDb.

这篇关于从文档中提取的PartitionKey与CreateItemAsync的标头中指定的键不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆