在使用.NET SDK的Azure DocumentDB中,我在调用ReplaceDocumentAsync时出现以下错误:
“错误”:“输入内容无效,因为缺少必需的属性- 'id;‘”,“请求负载无效。请确保提供有效的请求负载。”
这是一个博客帖子场景,当添加新评论时,我获得文档,添加评论并调用ReplaceDocumentAsync。我是这样做的:
string query = "SELECT * FROM Posts p WHERE p.id = 'some guid'";
var post = Client.CreateDocumentQuery<Post>(Collection.DocumentsLink, query)
.AsEnumerable().FirstOrDefault();
post.Comments.Add(comment);
Document doc = Client.CreateDocumentQuery(Collection.DocumentsLink)
.Where(d => d.Id == id)
.AsEnumerable()
.FirstOrDefault();
var document = await Client.ReplaceDocumentAsync(doc.SelfLink, item);
Post类:
public class Post
{
public Post()
{
Comments = new List<Comment>();
}
public Guid Id { get; set; }
public List<Comment> Comments { get; set; }
...
}
我做错了什么?
发布于 2015-04-19 15:42:35
好了,我想通了。
DocumentDB中的每个文档都需要有一个"id“属性。如果一个类没有,它将被分配一个并保存到文档中。由于DocumentDB区分大小写,我的"Id“只是另一个属性,一个单独的"id”被添加并分配给文档。
我通过删除并重新创建具有以下Id属性的所有文档修复了此问题:
[JsonProperty(PropertyName = "id")]
public Guid Id { get; set; }
发布于 2020-10-31 01:49:38
或者,您可以告诉Cosmos使用camelCasingWhichIsStandardForJson
new CosmosClient(
connectionstring,
new CosmosClientOptions(){
SerializerOptions = new CosmosSerializationOptions(){
PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
}
}
)
发布于 2017-01-20 20:46:18
正如Hossein所指出的,您可以修改您的类以将属性序列化为'id‘(区分大小写)。
或者,可以这样做:
dynamic json = JObject.FromObject(item);
json.id = doc.Id;
var document = await Client.ReplaceDocumentAsync(doc.SelfLink, json);
因此不会强制item实现它自己的“id”属性。
https://stackoverflow.com/questions/29725561
复制相似问题