1
votes

I'm trying to upload some data into Azure Cosmos Db container via CreateItemAsync() method, with this code:

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

The problem is, that upon trying to upload, i get "PartitionKey extracted from document doesn't match the one specified in the header" error mentioned in the title. I've read similar topics about this, and wasn't able to find out what's wrong. I'm trying to pass value as partitionKey that is defined as [JsonProperty(PropertyName = "id")], moreover, container I'm trying to upload into has been created by someone in azure portal, and I do not know what PartitionKey is specified. Upon trying to run

select c.partitionkey from c

In cosmos db, i get only this for 3 items that have been created manually via "New item" option:

[
    {},
    {},
    {}
]

Any ideas?

1
What API are using with CosmosDB ? Also, do you jnow the data schema used on the container ?MarleneHE
I'm using official microsoft nuget to access it: Microsoft.Azure.Cosmos in newest version. Also, the data structure of 3 items that reside in the container and have been uploaded manually, is different than the one I want co upload, if this is what you are asking, and I have access to them.Wolwgang
You can get PartitionKeyPath by using this code ContainerProperties properties = await container.ReadContainerAsync(); Console.WriteLine(properties.PartitionKeyPath);Steve Zhao
Steve answered all I needed :) After getting partitionKeyPath, i just had to pass variable with the same name that was after slash '/' as a partitionKey.Wolwgang

1 Answers

3
votes

This error causes by two value of PartitionKey(within your document and CreateItemAsync method) are not same.

You can get PartitionKeyPath by using this code

ContainerProperties properties = await container.ReadContainerAsync();             
Console.WriteLine(properties.PartitionKeyPath);

Then pass the value of the same name that was after slash '/' as a partitionKey to CreateItemAsync method can solve this error.