加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 尝试从Cosmos DB中删除时未找到资源

发布时间:2020-12-15 22:53:48 所属栏目:百科 来源:网络整理
导读:当我尝试删除Cosmos DB的资源时,我收到以下错误:找不到资源.当我开始使用带有分区键的无限集合时,它就开始发生了.这没有partionkey和限制10gb集合工作正常. protected async Taskbool DeleteDocument(Resource document) { var documentUri = UriFactory.Cr
当我尝试删除Cosmos DB的资源时,我收到以下错误:找不到资源.当我开始使用带有分区键的无限集合时,它就开始发生了.这没有partionkey和限制10gb集合工作正常.

protected async Task<bool> DeleteDocument(Resource document)
    {
        var documentUri = UriFactory.CreateDocumentUri(_db.Options.Value.DatabaseName,_db.Options.Value.CollectionName,document.Id);

        ResourceResponse<Document> result = null;

        var options = new RequestOptions
        {
            PartitionKey = new PartitionKey("moachingpartionkey")
        };

        for (int i = 0; i < MaxRetryCount; i++)
        {
            try
            {
                result = await _db.Client.DeleteDocumentAsync(documentUri,options);
                break;
            }
            catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429)
            {
                _logger.LogWarning($"");
                await Task.Delay(dex.RetryAfter);
            }
        }

        if (result == null)
            return false;

        int statusCode = (int)result.StatusCode;
        return statusCode >= 200 && statusCode < 300;
    }

这是我的创作:

protected async Task<bool> CreateDocumentAsync(Resource document)
    {
        var collectionUri = UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName,_db.Options.Value.CollectionName);

        ResourceResponse<Document> result = null;

        for (int i = 0; i < MaxRetryCount; i++)
        {
            try
            {
                result = await _db.Client.CreateDocumentAsync(collectionUri,document);
                break;
            }
            catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429)
            {
                _logger.LogWarning($"");
                await Task.Delay(dex.RetryAfter);
            }
        }

        if (result == null)
            return false;

        int statusCode = (int)result.StatusCode;
        return statusCode >= 200 && statusCode < 300;
    }

解决方法

由于您在评论中询问过,这里是我用来在创建集合时添加分区键的代码:

var collection = new DocumentCollection
{
    Id = "Customers",// just an example collection
};

// Set partition key
collection.PartitionKey.Paths.Add("/CountryId"); // just an example of the partition key path

// Set throughput
var options = new RequestOptions
{
    OfferThroughput = 400,// Default value is 10000. Currently set to minimum value to keep costs low.
};

// Create
await client.CreateDocumentCollectionIfNotExistsAsync(
   UriFactory.CreateDatabaseUri("YourCosomosDatabaseId"),collection,options);

这是我用来删除文档的代码.请注意,我先检查它是否存在,否则我会得到例外.

// Check if it exists,otherwise delete throws
 var doc = await GetByIdAsync(id,99); // your method to fetch the document by Id,the partition key (CountryId) is 99 
 if (doc == null)
 {
     return true; // Indicates successful deletion
 }

 // Delete
 var uri = UriFactory.CreateDocumentUri("YourCosomosDatabaseId","Customers",id);
 var reqOptions = new RequestOptions { PartitionKey = new PartitionKey(99) }; // CountryId is 99
 var result = await Client.DeleteDocumentAsync(uri,reqOptions);
 return result.StatusCode == HttpStatusCode.NoContent;

To clarify a bit of the terminology-
When you say PartitionKey you
imply a value like an int or string e.g 99 above.

Whereas when you say PartitionkeyPath you imply the property
path/name in the document e.g /CountryId in above.

参考文献IDocumentClient和DocumentClient

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读