c# - Azure Function - "PartitionKey extracted from document doesn' t 与在门户中创建的容器上的 header 中指定的匹配

标签 c# azure azure-functions azure-cosmosdb

我尝试通过 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/

相关文章:

c# - 如何从 ContentView 访问 Xamarin Forms App 对象

c# - 添加到 xmlnode 的未命名命名空间

azure - 我创建了哪种 Azure 存储帐户?

azure - 在 powerrshell 中执行 azure 命令而不将错误写入控制台?

Azure Function Host key 限制?

c# - MVC5 自定义绑定(bind) : List of Strings to List of Complex Type

c# - 如果它是数据表的第一行,如何增加变量

upload - Azure 的 Web 角色可以保持事件状态多长时间?

在 Azure Function 中运行 R.NET

azure - 使用托管服务身份会阻止在 Debug模式下连接到 KeyVault