.net core Cosmos DB Upsert故障

.net core Cosmos DB Upsert故障,.net-core,azure-cosmosdb,azure-cosmosdb-sqlapi,.net Core,Azure Cosmosdb,Azure Cosmosdb Sqlapi,正在尝试使用C#从Windows 10上的.NET Core 3.1控制台应用程序向Cosmos DB中插入项目 JSON文档如下所示: { "id": "25217f96-d399-4bb7-b8f1-4a7365cca76c", "title": "my title", "eventUtc": "2020-09-04T14:16:04+

正在尝试使用C#从Windows 10上的.NET Core 3.1控制台应用程序向Cosmos DB中插入项目

JSON文档如下所示:

{
        "id": "25217f96-d399-4bb7-b8f1-4a7365cca76c",
        "title": "my title",
        "eventUtc": "2020-09-04T14:16:04+0000" ,
        "listOne":
                        [
                                {"person1" : "james" , "contributorContact" : "" , "contributorTtype" : "contributor"} ,
                                {"person2" : "veena" , "contributorContact" : "" , "contributorTtype" : "contributor"}
                        ]
        ,
        "listTwo" :
                        [
                                "lorem" , "ipsum"  , "dolor" 
                        ] ,
        "synopsis" : "abcdefghi."  }
容器中不存在此文档,但容器中已存在另一个文档。分区键是“id”

此代码失败:

文件不多,因此需要单独升级它们

错误消息是:

响应状态代码不表示成功:BadRequest(400); 子状态:0;活动ID:9c545c05-102f-4ed5-9c6c-e5092ae1671b; 原因:(消息:{“Errors”:[“指定的输入之一是 无效“]}

代码中的错误在哪里?根据我的想法,这会起作用


谢谢。

在您提供的代码中,似乎您发送的是分区键名称“id”,而不是项目的
id
。它应该是:

foreach (var fileOne in Directory.GetFiles(fileLoc))
{
    MemoryStream stream = new MemoryStream();
    await System.Text.Json.JsonSerializer.SerializeAsync(stream, File.ReadAllText(fileOne));
    string itemId = // Extract ID from item
    var response = await cosmosContainer.UpsertItemStreamAsync(stream, new PartitionKey(itemId));
}

注意:在提供分区键时使用已解析的
itemId
。此外,您可能希望在MemoryStream实例中使用
using
语句。

在您提供的代码中,似乎您发送的是分区键名称“id”,而不是项的
id
。它应该是:

foreach (var fileOne in Directory.GetFiles(fileLoc))
{
    MemoryStream stream = new MemoryStream();
    await System.Text.Json.JsonSerializer.SerializeAsync(stream, File.ReadAllText(fileOne));
    string itemId = // Extract ID from item
    var response = await cosmosContainer.UpsertItemStreamAsync(stream, new PartitionKey(itemId));
}

请注意,在提供分区键时使用已解析的
itemId
。此外,您可能希望在MemoryStream实例中使用
语句。

此代码返回错误“BadRequest(400);Substatus:0;(消息:{“Errors”:[“指定的输入之一无效”]}”var itemId=“25217f96-d399-4bb7-b8f1-4a7365cca76c”;使用(var memoryStream=new memoryStream()){await JsonSerializer.SerializeAsync(memoryStream,File.ReadAllText(fileOne));memoryStream.Seek(0,SeekOrigin.Begin);var response=await cosmosContainer.upCertItemStreamAsync(memoryStream,new PartitionKey(itemId));})我会怀疑原始数据,首先将其反序列化到类型化对象中,然后再将其用作要插入的项。这样,您可以检查它是否正确读取。我创建了一个类型,并使用Newtonsoft属性对其进行修饰,然后使用Newtonsoft反序列化器,然后插入的项很好。为了速度起见,我希望这样做,无需将文件转换为字符串以插入对象,因为如果我访问Azure门户并复制粘贴文档的文本,它只需插入即可。哦,很好。感谢帮助!此代码返回错误“BadRequest(400);子状态:0;(消息:{“Errors”:[“指定的输入之一无效”]}”var itemId=“25217f96-d399-4bb7-b8f1-4a7365cca76c”;使用(var memoryStream=new memoryStream()){await JsonSerializer.SerializeAsync(memoryStream,File.ReadAllText(fileOne));memoryStream.Seek(0,SeekOrigin.Begin);var response=await cosmoconcontainer.upsitItemStreamasync(memoryStream,new PartitionKey(itemId));})我会怀疑原始数据,首先将其反序列化到类型化对象中,然后再将其用作要插入的项。这样,您可以检查它是否正确读取。我创建了一个类型,并使用Newtonsoft属性对其进行修饰,然后使用Newtonsoft反序列化器,然后插入的项很好。为了速度起见,我希望这样做,无需将文件转换为字符串并插入对象,因为如果我访问Azure门户并复制粘贴文档的文本,它只需插入即可。哦,好吧,谢谢您的帮助!