Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

azure cosmosdb - PartitionKey extracted from document doesn't match the one specified in the header on CreateItemAsync

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...