Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未找到Azure Cosmos数据库中的资源_C#_Azure_Azure Cosmosdb - Fatal编程技术网

C# 未找到Azure Cosmos数据库中的资源

C# 未找到Azure Cosmos数据库中的资源,c#,azure,azure-cosmosdb,C#,Azure,Azure Cosmosdb,您好,我们在查询时遇到问题。该文档存在于数据库中 “消息:{\'Errors\':[\'Resource Not Found\']}\r\n活动ID: 03866338-6596-49b6-8704-1726cb373bfb,请求URI: /APP/ab277caf-ee90-4cc3-96cb-4d4ec5ae2b13/服务/17e48284-a3a0-40c5-b5ec-40bd3f207472/分区/27cb7777-5add-4f72-8a73-1fc8fe34e7bf/副本/13160

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

“消息:{\'Errors\':[\'Resource Not Found\']}\r\n活动ID: 03866338-6596-49b6-8704-1726cb373bfb,请求URI: /APP/ab277caf-ee90-4cc3-96cb-4d4ec5ae2b13/服务/17e48284-a3a0-40c5-b5ec-40bd3f207472/分区/27cb7777-5add-4f72-8a73-1fc8fe34e7bf/副本/131603393672093060p/, RequestStats:,SDK:Microsoft.Azure.Documents.Common/1.19.162.2“

数据库中的文档

{
    "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
}
下面是引发此错误的更新方法代码

{
            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;
            }
        }

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

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

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

这里的分区键是我的集合的“name”。您可以检查集合的分区密钥

我的文件如下:

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

{
    "id": "2",
    "name": "jay2"
}
我的
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;
   }
希望对你有帮助


更新答案:

我创建了一个与您相同的示例文档,并成功地替换了它。

请参考我的测试代码

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; }
    }
}
使用Microsoft.Azure.Documents;
使用Microsoft.Azure.Documents.Client;
使用制度;
使用System.Linq;
名称空间控制台EApp2
{
班级计划
{
私有静态文档客户端;
静态字符串端点=“***”;
静态字符串键=“***”;
静态字符串数据库=“***”;
静态字符串集合=“***”;
静态void Main(字符串[]参数)
{
client=newdocumentclient(新Uri(端点),键);
尝试
{
示例querysample=client.CreateDocumentQuery(
CreateDocumentCollectionUri(数据库,集合))
.其中(so=>so.id==“1”)
.可计算的()
.First();
Console.WriteLine(querysample.tablename);
querysample.tablename=“Table2”;
RequestOptions=newrequestoptions();
options.PartitionKey=新的PartitionKey(“1”);
options.consistentylevel=consistentylevel.Session;
var result=client.replaceDocumentSync(UriFactory.CreateDocumentUri(数据库,集合,“1”),querysample,options);
}
捕获(例外情况除外)
{
掷骰子;
}
Console.ReadLine();
}
}
公共类样本
{
公共字符串id{get;set;}
公共字符串表名{get;set;}
}
}
id
是我的分区键,值是
'1'
。请检查一下我们的代码之间的差异好吗


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

您好,现在有什么更新吗?我的答案对你有帮助吗?嗨,杰,谢谢你的检查,我检查了我的代码,我正在传递正确的分区值。所以问题仍然存在…嗨,纳文。请看我的更新答案。代码对我来说很有效。如果有任何问题,请告诉我。@Hardrada非常感谢您的肯定。