为了账号安全,请及时绑定邮箱和手机立即绑定

使用 C# 和 cosmos 客户端从 cosmos 数据库中删除文档

使用 C# 和 cosmos 客户端从 cosmos 数据库中删除文档

C#
收到一只叮咚 2023-07-22 15:58:40
我是 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,“patentId”:null,“orchestrationId”:null,“preOrderId”:“88557ec1-dccb-4397-9ce4-20b6aeb1d636”,“cartId”:“adc463ef-7f38-4fa8-8f37-e505db3a5134”,“confirmationNum”:“QDQ-9 244014-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 Substatus:0 原因:()。;演示结束,按任意键退出。
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

问题出在你的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);

}


查看完整回答
反对 回复 2023-07-22
  • 1 回答
  • 0 关注
  • 79 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信