无法使用microsoft.azure.documentdb.core更新Azure CosmosDB中的文档

问题描述 投票:0回答:1

我正在尝试使用microsoft.azure.documentdb.core包提供的ReplaceDocumentAsyc方法更新Azure CosmosDB集合中的文档。调用ReplaceDocumentAsyc时,我得到200响应,但集合中的文档本身没有得到更新。以下是简要设置(代码的相关部分)供参考。

    private DocumentClient Client
    {
        get
        {
            if (_client == null)
            {
                _client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
            }

            return _client;
        }
    }

    Client.ReplaceDocumentAsync(documentSelfLink, doc).ContinueWith(response =>
    {
        return new QueryResponse { Success = response.Result.StatusCode == HttpStatusCode.OK, LastChanged = lastChanged };
    });

代码对我来说似乎很好,我相信它之前能够更新集合。还有其他人遇到类似问题吗?

database .net-core nosql azure-cosmosdb
1个回答
2
投票

请参考我的工作代码:

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using System;

namespace JayTestDocumentDB
{
    class Program
    {
        private static DocumentClient client;
        private static string EndpointUrl = "https://***.documents.azure.com:443/";
        private static string AuthorizationKey = "***";
        private static string databaseId = "db";
        private static string collectionId = "coll";

        static void Main(string[] args)
        {
            client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
            var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

            var options = new FeedOptions
            {
                MaxItemCount = 100
            };      
            FeedResponse<Document> doc = client.CreateDocumentQuery<Document>(uri, options).AsDocumentQuery().ExecuteNextAsync<Document>().Result;

            foreach (Document d in doc)
            {
                Console.WriteLine(d);
                d.SetPropertyValue("name","jay");

                client.ReplaceDocumentAsync(d.SelfLink, d).ContinueWith(response =>
                {
                    Console.WriteLine(response.Result.StatusCode);
                    //return new QueryResponse { Success = response.Result.StatusCode == HttpStatusCode.OK, LastChanged = lastChanged };
                });
                break;
            }
            Console.ReadLine();
        }
    }                    
}

我的microsoft.azure.documentdb.core包版本是2.0.0-preview2

有任何疑虑,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.