【发布时间】:2021-01-16 17:38:08
【问题描述】:
我在尝试从 CosmosDb 删除文档时遇到问题,据我了解,我首先必须加载要删除的文档,然后将 document.SelfLink 传递给以下方法:
DeleteDocumentAsync
但是,当我尝试删除它时,会引发以下错误:
系统中不存在指定id的实体
我知道该文档存在,因为我可以在 document 变量中看到该文档。
以下是我当前的代码设置:
private static void DeleteDocument()
{
var feedOptions = new FeedOptions
{
MaxItemCount = -1,
EnableCrossPartitionQuery = true
};
var document = _documentClient
.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(_databaseName, _collection), feedOptions)
.Where(x => x.Id == "ba00d500-de61-411e-b8ef-f749b23cc326")
.AsEnumerable()
.SingleOrDefault();
if (document == null) return; // Document is not null when I check here
_documentClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(_databaseName, _collection, document.Id), new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey(document.Id) })
.GetAwaiter()
.GetResult();
}
这是 CosmosDb 中的文档:
我也尝试过引用document.SelfLink 而不是使用UriFactory.CreateDocumentUrl,但结果仍然相同。
更新。
似乎在 Undefined.Value 中传递 PartitionKey 似乎有效:
_documentClient.DeleteDocumentAsync(document.SelfLink, new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey(Undefined.Value) })
.GetAwaiter()
.GetResult();
我个人觉得很奇怪。
【问题讨论】:
-
文档有
id(小写)但你的Linq谓词有Id(你引用document.Id)
标签: c# azure-cosmosdb