gpt4 book ai didi

azure-cosmosdb - 指定的输入之一无效

转载 作者:行者123 更新时间:2023-12-05 08:31:23 25 4
gpt4 key购买 nike

使用 nuget 包“Microsoft.Azure.Cosmos”(版本 3.4.1)我无法对非常基本的模型(三个属性)执行更新插入操作。我查看了 cosmos 样本并将它们作为起点,但无法弄清楚我做错了什么

当尝试执行以下代码时,我收到以下错误“指定的输入之一无效”

完整代码

public class CatalogModel
{
[JsonProperty("id")]
public long Id { get { return ModelId; } }

[JsonProperty("ModelId")]
public long ModelId { get; set; }

public string Name { get; set; }
}

public async Task Do(CatalogModel model)
{
string databaseId = "<dbid_here>"; // in real application this will not be hard coded
string containerId = "<containerid_here>"; // in real application this will not be hard coded
Database database = await _client.CreateDatabaseIfNotExistsAsync(databaseId);

// Delete the existing container to prevent create item conflicts
using (await database.GetContainer(containerId).DeleteContainerStreamAsync())
{ }

// We create a partitioned collection here which needs a partition key. Partitioned collections
// can be created with very high values of provisioned throughput (up to Throughput = 250,000)
// and used to store up to 250 GB of data. You can also skip specifying a partition key to create
// single partition collections that store up to 10 GB of data.
ContainerProperties containerProperties = new ContainerProperties(containerId, partitionKeyPath: "/ModelId");

// Create with a throughput of 1000 RU/s
Container _container = await database.CreateContainerIfNotExistsAsync(
containerProperties,
throughput: 1000);

try
{
var id = model.ModelId.ToString();
var partitionKey = new PartitionKey(id);


var response = await _container.ReadItemStreamAsync(
id: id,
partitionKey: partitionKey,
cancellationToken: context.CancellationToken);

ItemResponse<CatalogModel> itemResponse;
if (response.IsSuccessStatusCode)
{
//TODO: implement
}
else
{
var item = model
// This is not working either
//itemResponse = await _container.UpsertItemAsync<CatalogModel>(
// item,
// partitionKey: partitionKey,
// cancellationToken: context.CancellationToken);
using (Stream stream = ToStream<CatalogModel>(item))
{
string body;
using (var reader = new StreamReader(stream, new UTF8Encoding(false, true), false, 1024, true))
{
body = await reader.ReadToEndAsync();
}
stream.Seek(0, SeekOrigin.Begin);
using (ResponseMessage responseMessage = await _container.UpsertItemStreamAsync(
partitionKey: partitionKey,
streamPayload: stream))
{
using (var reader = new StreamReader(responseMessage.Content, new UTF8Encoding(false, true), false, 1024, true))
{
body = await reader.ReadToEndAsync();
}
}
}

}
}
catch (Exception ex)
{

throw;
}

dbug information

最佳答案

有两个步骤可以使您的代码正常工作。

  1. id 需要是一个string。您可以按照@Matias Quaranta 的回答更改您的代码。

  2. 传递正确的 partitionKey。正如您定义为 long 类型,但您传递了一个 string 值。

使用 var partitionKey = new PartitionKey(model.ModelId); 而不是 var partitionKey = new PartitionKey(id);

Step2 将解决如下问题。

enter image description here

关于azure-cosmosdb - 指定的输入之一无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59060351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com