使用 nuget 包“Microsoft.Azure.Cosmos”(版本 3.4.1)我无法对非常基本的模型(三个属性)执行 upsert 操作。我查看了宇宙样本并将它们用作起点,但无法弄清楚我做错了什么
当尝试执行以下代码时,我收到以下错误 “指定的输入之一无效”。
完整代码
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;
}
Run Code Online (Sandbox Code Playgroud)
id需要是一个string. 您的模型可能需要是:
public class CatalogModel
{
[JsonProperty("id")]
public string Id { get { return ModelId.ToString(); } }
[JsonProperty("ModelId")]
public long ModelId { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有两个步骤可以让您的代码正常工作。
id需要是一个string. 您可以将代码更改为@Matias Quaranta 的答案。
传递正确的partitionKey。正如您定义为long类型,但您传递了一个字符串值。
使用var partitionKey = new PartitionKey(model.ModelId);而不是 varpartitionKey = new PartitionKey(id);
Step2 将解决如下问题。
有同样的问题。看起来Id可以是其他类型,我自己使用一个guid。我有一个记录看起来像
public record Data(Guid Id, string Field1);
Run Code Online (Sandbox Code Playgroud)
在 C# 中,命名标准有点强制名称与 CosmosDB 期望的不匹配。你确实有能力更改命名策略,使其更好地与 C# 标准一起工作...实际上,这应该是默认值
var client = new CosmosClient(
connectionString,
new CosmosClientOptions
{
SerializerOptions = new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase },
});
Run Code Online (Sandbox Code Playgroud)
现在,您的字段“Id”已映射到“id”,并且错误消失了。
| 归档时间: |
|
| 查看次数: |
14492 次 |
| 最近记录: |