17

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

upon running

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

it throws

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"]}

but if I put,

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

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

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;

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?

Jan_V
  • 4,244
  • 1
  • 40
  • 64
Gabriel P.
  • 3,400
  • 2
  • 32
  • 23
  • 1
    you can specify any field from your JSON document to be partition key not necessarily "id". Partition Key specified while creating the container should be the key value that is passed while creating item. In your case Id is the partition key that was specified while creating container hence it worked when you passed the "id" . Also note - While creating partition key create it in "camelCase" format. e.g. you can specify "pinCode" as partition key – Varun Mar 15 '21 at 12:48

3 Answers3

36

I've overlooked that when I have created the container

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

I have specified partitionKeyPath as beeing the /id.

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

if partitionKeyPath is /partition

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

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

Gabriel P.
  • 3,400
  • 2
  • 32
  • 23
  • 2
    Note that, when you create the Container in the Azure Portal, which requires you to specify a PartitionKey as well, you can also directly refer to a property of the documents you want to insert in that container later on. For instance, when your container will store `Person` documents and the `Person` class has a property `FirstName` you can specify the PartitionKey in the portal as `/FirstName`. In this case, Json annotation `[JsonProperty()` of the property is not needed. – JJuice May 03 '20 at 11:45
  • 1
    Thank you, this was helpful! – lightw8 May 17 '20 at 21:05
  • 3
    Ahh, I didn't realize it had to be a string; my partition key property was of type PartitionKey (and the two exactly matched, and it was specified correctly and everything)... switching it to a string fixed the issue. ‍♂️ – BrainSlugs83 Feb 09 '21 at 01:03
  • 1
    Thanks so much @BrainSlugs83 - I've been fighting this for a while (before you made this comment) came back to it tonight with fresh eyes, found this, solved in 10 minutes. – Jynx Feb 16 '21 at 21:55
  • 1
    More about choosing partition key - https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview#choose-partitionkey – Varun Mar 01 '21 at 11:24
  • @BrainSlugs83 The partion key value can be ["of string or numeric types."](https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview#choose-partitionkey) But the document is not clear as one would wish on this. My test result is that intigers and string seem working fine while char oddly doesn't. – DaPanda Aug 31 '21 at 05:50
  • I had this exact problem. This answer was super helpful and saved me a ton of time. – Matthew David Jankowski Mar 18 '22 at 15:17
0

Just had a similar issue. In my case the type did not match. In some documents the property I used as a partitionKey had a string value and in others it had an int value.

Mihai Toth
  • 11
  • 1
0

Rember that CosmosDb is case sensitive. Meaning that if you inferred serilization and upsert, the property names in you document will be updated to match the capitalization of your class, unless you annotate your properties.

xtreampb
  • 528
  • 6
  • 19