C# 使用C和cosmos客户端从cosmos数据库中删除文档
我是Cosmos DB的新手,在阅读了microsoft文档后,我试图从Cosmos DB中删除记录。我已经尽可能地模仿这个例子。正在查找和读取该记录,但在尝试删除时遇到“未找到”错误 集合是分区的,我在示例中提到了分区键,但仍然会导致错误C# 使用C和cosmos客户端从cosmos数据库中删除文档,c#,azure-cosmosdb,delete-row,azure-cosmosdb-sqlapi,C#,Azure Cosmosdb,Delete Row,Azure Cosmosdb Sqlapi,我是Cosmos DB的新手,在阅读了microsoft文档后,我试图从Cosmos DB中删除记录。我已经尽可能地模仿这个例子。正在查找和读取该记录,但在尝试删除时遇到“未找到”错误 集合是分区的,我在示例中提到了分区键,但仍然会导致错误 private async Task QueryItemsAsync() { var sqlQueryText = "SELECT * FROM c WHERE c.tenantId = '5c6cb2d77c1c2edc00
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,patientId:null,orchestrationId:null,预订单号:88557ec1-dccb-4397-9ce4-20b6aeb1d636,cartId:adc463ef-7f38-4fa8-8f37-e505db3a5134,确认号:QDQ-9244014-14708,cartConfirmationNum:IBM-8622967-14079,国家代码:null,类型:订单,取消日期:null,Id:abae8abf-5836-464f-8129-1f0e83a1f639}}
发生NotFound错误:CosmosRequestException;StatusCode=NotFound;子状态代码=0;ActivityId=6dd52d23-68c8-4e6d-b4ff-70cd2a6921cf;请求费用=1.24;消息=响应状态代码不表示成功:404子状态: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);
}
问题在于您的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);
}
我试着按照这个例子做一个T。你试着在data explorer上执行查询并得到结果了吗?我试着按照这个例子做一个T。你试着在data explorer上执行查询并得到结果了吗?谢谢你,尼克,我能够删除它。谢谢你,尼克,我能够删除它。