![](/img/trans.png)
[英]Azure Function - "PartitionKey extracted from document doesn't match the one specified in the header" on container created in portal
[英]CosmosDb - 'PartitionKey extracted from document doesn't match the one specified in the header'
我已经为这个问题寻找了类似的答案,但他们似乎都没有为我解决这个问题。
尝试针对 cosmosDb 模拟器运行以下代码时,我PartitionKey extracted from document doesn't match the one specified in the header
...
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));
我看不出有什么问题。 我的分区键路径看起来正确,分区键的值也正确。
事件 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; }
}
参考: https://docs.microsoft.com/azure/cosmos-db/troubleshoot-bad-request#wrong-partition-key-value
这意味着您的 Container/Collection 没有按您可能认为从代码角度来看的属性进行分区。 当您发送正文和 PartitionKey 参数时,Cosmos DB 后端将检查正文中定义为容器/集合中分区键路径的属性,提取该值,并将其与 PartitionKey 参数进行比较(如果它们不匹配) ,你得到这个错误。
例如,如果您的 Container/Collection 的 Partition Key Path 为/Artist
,由于您将 CategoryId 作为 PartitionKey 传递,因此 body 属性(Artist 的值)的提取与其他参数不匹配。
如果您的文档使用不同的大小写进行序列化,也会发生这种情况(例如,您使用/categoryId
创建容器/集合,并且您的应用程序将 Model 序列化为CategoryId
,因此categoryId
的正文值实际上是null
因为您没有通过为该外壳序列化的任何属性)。
聚会有点晚了,但我遇到了完全相同的错误,发现CosmosSerializationOptions
是罪魁祸首。 尝试禁用该部分,它可能会有所帮助。
当然,它们的存在是有原因的,但这可能与配置不匹配有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.