![](/img/trans.png)
[英]CosmosDb - 'PartitionKey extracted from document doesn't match the one specified in the header'
[英]Azure Function - "PartitionKey extracted from document doesn't match the one specified in the header" on container created in portal
我正在尝试通过 CreateItemAsync() 方法使用以下代码将一些数据上传到 Azure Cosmos Db 容器中:
Database partnersDb = GetCosmosDb();
Container partnersContainer = GetCosmosContainer(partnersDb);
try
{
ItemResponse<PartnerInfo> partnersResponse = await partnersContainer.CreateItemAsync(partner, new PartitionKey(partner.Id));
}
catch (CosmosException e) when (e.StatusCode == HttpStatusCode.Conflict)
{
logger.LogInformation($"Item with id {partner.Id} already exists in partners database");
}
问题是,在尝试上传时,我收到标题中提到的“从文档中提取的 PartitionKey 与标头中指定的不匹配”错误。 我已经阅读过类似的主题,但无法找出问题所在。 我正在尝试将值作为定义为[JsonProperty(PropertyName = "id")]
的 partitionKey 传递,此外,我要上传到的容器是由 azure 门户中的某人创建的,我不知道 PartitionKey 是什么被指定。 尝试运行时
select c.partitionkey from c
在 cosmos db 中,对于通过“新项目”选项手动创建的 3 个项目,我只得到这个:
[
{},
{},
{}
]
有任何想法吗?
此错误是由PartitionKey
的两个值(在您的文档和CreateItemAsync
方法中)不相同引起的。
您可以使用此代码获取PartitionKeyPath
ContainerProperties properties = await container.ReadContainerAsync();
Console.WriteLine(properties.PartitionKeyPath);
然后将斜杠'/'后的同名值作为partitionKey传给CreateItemAsync
方法即可解决该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.