我尝试通过 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 与 header 中指定的不匹配”错误。我读过有关此的类似主题,但无法找出问题所在。我正在尝试将值作为定义为 [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
方法可以解决此错误。
关于c# - Azure Function - "PartitionKey extracted from document doesn' t 与在门户中创建的容器上的 header 中指定的匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64007032/