未找到资源 Azure Cosmos DB

Nav*_*wal 3 c# azure azure-cosmosdb

嗨,我们在查询时遇到问题。该文档存在于数据库中。

"消息:{\"错误\":[\"未找到资源\"]}\r\nActivityId: 03866338-6596-49b6-8704-1726cb373bfb,请求 URI:/apps/ab277caf-ee90-4cc3-96cb-24d3 /services/17e48284-a3a0-40c5-b5ec-40bd3f207472/partitions/27cb7777-5add-4f72-8a73-1fc8fe34e7bf/replicas/131603393670s.20393670s.sdk.sdk.207472/partitions/27cb7777-5add-4f72-8a73-1fc8fe34e7bf/replicas/131603393670s.2093670s.sdk.1.2.1.2.1.SDK.

数据库中的文档

{
    "consumername": "testconsumer",
    "tablename": "Table1",
    "securityaccount": "v-naagga",
    "logtime": "2018-01-13T21:42:21.3040338-08:00",
    "securitydefinition": {
        "tablename": "table1",
        "ColumnList": {
            "columnname": "name",
            "columndatatype": "string"
        },
        "RowSecurity": {
            "columnname": "address",
            "operator": "operator",
            "condition": "somecondition"
        }
    },
    "id": "15554839-096d-4072-8f38-af2e9c64b452",
    "_rid": "LmUiAONSDQQBAAAAAAAAAA==",
    "_self": "dbs/LmUiAA==/colls/LmUiAONSDQQ=/docs/LmUiAONSDQQBAAAAAAAAAA==/",
    "_etag": "\"00002e04-0000-0000-0000-5a5aedd60000\"",
    "_attachments": "attachments/",
    "_ts": 1515908566
}
Run Code Online (Sandbox Code Playgroud)

以下是引发此错误的更新方法代码

{
            try
            {
                RequestOptions options = new RequestOptions();
                options.PartitionKey = new PartitionKey(id);
                options.ConsistencyLevel = ConsistencyLevel.Session;
                return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, SecurityCollectionId, id), item,options).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.Log(ErrorLevel.Error, ex.Message);
                throw ex;
            }
        }
Run Code Online (Sandbox Code Playgroud)

Jay*_*ong 6

根据我的观察,我认为您的问题应该是分区键设置错误。

请参考此官方文档。您需要提供分区键的值,而不是存储分区键的字段名称。

例如,我的容器是这样创建的:

在此处输入图片说明

此处的分区键是我收藏的“名称”。您可以检查集合的分区键。

我的文件如下:

{
    "id": "1",
    "name": "jay"
}

{
    "id": "2",
    "name": "jay2"
}
Run Code Online (Sandbox Code Playgroud)

partitionkey'name',所以这里我有两个分区:'jay''jay1'

因此,在这里您应该将partitionkey属性设置为“jay”或“jay2”,而不是“name”。

try
   {
     RequestOptions options = new RequestOptions();
     options.PartitionKey = new PartitionKey("jay");
     options.ConsistencyLevel = ConsistencyLevel.Session;
     return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, SecurityCollectionId, id), item,options).ConfigureAwait(false);
   }
     catch (Exception ex)
   {
     Logger.Log(ErrorLevel.Error, ex.Message);
     throw ex;
   }
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助。


更新答案:

我创建了一个与您相同的示例文档并成功替换了它。 在此处输入图片说明

请参考我的测试代码。

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

namespace ConsoleApp2
{
    class Program
    {

        private static DocumentClient client;

        static string endpoint = "***";
        static string key = "***";
        static string database = "***";
        static string collection = "***";
        static void Main(string[] args)
        {

            client = new DocumentClient(new Uri(endpoint), key);

            try
            {
                Sample querysample = client.CreateDocumentQuery<Sample>(
                UriFactory.CreateDocumentCollectionUri(database, collection))
                .Where(so => so.id == "1")
                .AsEnumerable()
                .First();

                Console.WriteLine(querysample.tablename);

                querysample.tablename = "Table2";

                RequestOptions options = new RequestOptions();
                options.PartitionKey = new PartitionKey("1");
                options.ConsistencyLevel = ConsistencyLevel.Session;
                var result =  client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(database, collection, "1"), querysample, options).Result;
            }
            catch (Exception ex)
            {
                throw ex;
            }


            Console.ReadLine();
        }
    }

    public class Sample
    {
        public string id { get; set; }
        public string tablename { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

id是我的分区键,值为'1'. 你能检查一下我们代码之间的差异吗?

如果有任何问题,请告诉我。