1
votes

I've looked for similar answers to this issue but none of them seem to fix this issue for me.

I'm getting a PartitionKey extracted from document doesn't match the one specified in the header when attempting to run the code below against the cosmosDb emulator...

CosmosClient cosmosClient = new CosmosClient("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", new CosmosClientOptions()
{
    SerializerOptions = new CosmosSerializationOptions()
    {
        PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
    }
});

var database = await cosmosClient.CreateDatabaseIfNotExistsAsync("EventCatalogDb");

var newContainer = await database.Database.CreateContainerIfNotExistsAsync("Events", "/CategoryId");

var CategoryConcert = new Category
{
    Id = Guid.Parse("b0788d2f-8003-43c1-92a4-edc76a7c5dde"),
    Name = "Concerts"
};

var myEvent = new Event
{
    Id = Guid.Parse("b419a7ca-3321-4f38-be8e-4d7b6a529319"),
    Name = "Clash of the DJs",
    Price = 85,
    Artist = "DJ 'The Mike'",
    Date = new DateTime(1466424490000),
    Description = "DJs from all over the world will compete in this epic battle for eternal fame.",
    ImageUrl = "https://gillcleerenpluralsight.blob.core.windows.net/files/GloboTicket/dj.jpg",
    CategoryId = CategoryConcert.Id.ToString(),
    CategoryName = CategoryConcert.Name
};

var eventResponse = await newContainer.Container.CreateItemAsync(myEvent, new PartitionKey(myEvent.CategoryId));

I can't see what is wrong. My partition key path looks correct as does the value for the partition key.

Edit

Event class

public class Event : IEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string CategoryId { get; set; }
    public string CategoryName { get; set; }
    public int Price { get; set; }
    public string Artist { get; set; }
    public DateTime Date { get; set; }
    public string Description { get; set; }
    public string ImageUrl { get; set; }
}
1
Could you also share the Event class?Arca Artem
@ArcaArtem AddedKonzy262
Is it possible that your collection was created earlier with a different partition key than the one you're using now? Do you still get the same error if you delete the collection on the emulator and running your code again?Arca Artem
@ArcaArtem I have tried blitzing everything before running. I've edited the post again and added the remaining cosmos bits that I'm using. I didn't think they were relevant but that's now everything I'm using from the client library.Konzy262
Not 100% sure but the partition key casing when defining on the collection might be the issue here. You've defined a camel case naming policy for your properties which implies a lowercase first letter when mapping entities to property names on Cosmos, but you've defined the partition key using an upper case first letter. Try changing CreateContainerIfNotExistsAsync("Events", "/CategoryId"); to CreateContainerIfNotExistsAsync("Events", "/categoryId"); to see if that'll helpArca Artem

1 Answers

0
votes

Reference: https://docs.microsoft.com/azure/cosmos-db/troubleshoot-bad-request#wrong-partition-key-value

This means that your Container/Collection is not partitioned by the attribute you might think it is from the code perspective. When you send the body and the PartitionKey parameter, the Cosmos DB backend will check the body for the property defined as Partition Key Path in the Container/Collection, extract that value, and compare it against the PartitionKey parameter, if they don't match, you get this error.

For example, if your Container/Collection had a Partition Key Path of /Artist, since you are passing CategoryId as as PartitionKey, the extraction of the body attribute (value of Artist) does not match with the other parameter.

This can also happen if your document is being serialized with different casing (for example, you create the Container/Collection with /categoryId and your application is serializing the Model as CategoryId, so the body value of categoryId is actually null because you are not passing any attribute serialized for that casing).