我是Cosmos DB的新手,在阅读了Microsoft文档后,我试图从cosmos db中删除记录。我试图尽可能地遵循该示例。正在查找和读取记录,但是尝试删除时遇到了未找到的错误。
集合已分区,我已经在示例中提到了分区键,但是仍然会导致错误。
private async Task QueryItemsAsync()
{
var sqlQueryText = "SELECT * FROM c WHERE c.tenantId = '5c6cb2d77c1c2edc001b9007' and c.id = 'abae8abf-5836-464f-8129-1f0e83a1f639'";
QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);
FeedIterator<Order> queryResultSetIterator = this.container.GetItemQueryIterator<Order>(queryDefinition);
List<Order> orders = new List<Order>();
while (queryResultSetIterator.HasMoreResults)
{
FeedResponse<Order> currentResultSet = await queryResultSetIterator.ReadNextAsync();
foreach (Order o in currentResultSet)
{
orders.Add(o);
Console.WriteLine("\tRead {0}\n", o);
}
}
}
private async Task DeleteFamilyItemAsync()
{
var partitionKeyValue = "tenantId";
var orderId = "abae8abf-5836-464f-8129-1f0e83a1f639";
// Delete an item. Note we must provide the partition key value and id of the item to delete
ItemResponse<Order> orderResponse = await this.container.DeleteItemAsync<Order>(orderId, new PartitionKey(partitionKeyValue));
Console.WriteLine("Deleted Family [{0},{1}]\n", partitionKeyValue, orderId);
}
如下所示,正在读取记录,因此可以找到它,但是当我尝试删除它时,并没有删除它。 读取{“ Id”:null,“病人ID”:null,“ orchestrationId”:null,“ preOrderId”:“ 88557ec1-dccb-4397-9ce4-20b6aeb1d636”,“ cartId”:“ adc463ef-7f38-4fa8-8f37-e505db3a5134 “,” confirmationNum“:” QDQ-9244014-14708“,” cartConfirmationNum“:” IBM-8622967-14079“,” countryCode“:null,” type“:” order“,” cancelDate“:null,” id“: “ abae8abf-5836-464f-8129-1f0e83a1f639”}
发生NotFound错误:CosmosRequestException; StatusCode = NotFound; SubStatusCode = 0; ActivityId = 6dd52d23-68c8-4e6d-b4ff-70cd2a6921cf; RequestCharge = 1.24; Message =响应状态代码不表示成功:404子状态:0原因:( )。 演示结束后,按任意键退出。
答案 0 :(得分:0)
问题出在您的partitionKeyValue
上。您正在提供分区键定义而不是值。该值是实际的租户ID,而不是文本tenantId
。
您的删除代码应如下所示:
private async Task DeleteFamilyItemAsync()
{
var partitionKeyValue = "5c6cb2d77c1c2edc001b9007"; // <-- This is the change
var orderId = "abae8abf-5836-464f-8129-1f0e83a1f639";
// Delete an item. Note we must provide the partition key value and id of the item to delete
ItemResponse<Order> orderResponse = await this.container.DeleteItemAsync<Order>(orderId, new PartitionKey(partitionKeyValue));
Console.WriteLine("Deleted Family [{0},{1}]\n", partitionKeyValue, orderId);
}