Error in DocumentDBDocument

In Azure DocumentDB using the .NET SDK, I get the following error when calling ReplaceDocumentAsync:

"Errors": ["Invalid input because the required properties are" id; "- none," "Invalid request payload. Make sure you provide a valid request payload."

This is a blog script when a new comment is added, I get a document, add a comment and call ReplaceDocumentAsync. Here is how I do it:

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); 

Publication Class:

 public class Post { public Post() { Comments = new List<Comment>(); } public Guid Id { get; set; } public List<Comment> Comments { get; set; } ... } 

What am I doing wrong?

+5
source share
3 answers

OK, I get it.

Each document in DocumentDB must have an id property. If the class does not have one, it will be assigned one and saved in the document. When DocumentDB is case sensitive, my "Identifier" was just another property, and a separate "id" was added and assigned to the document.

I fixed the problem by deleting and re-creating all my documents with the following attribute for Id:

 [JsonProperty(PropertyName = "id")] public Guid Id { get; set; } 
+10
source

As Hossein noted, you can change your class to serialize the property to "id" (case sensitive).

Alternatively, you can do something like this:

 dynamic json = JObject.FromObject(item); json.id = doc.Id; var document = await Client.ReplaceDocumentAsync(doc.SelfLink, json); 

Thus, without forcing the element to implement its own id property.

0
source

You must check ID = null

 Document doc = Client.CreateDocumentQuery(Collection.DocumentsLink) .Where(d => d.Id != null) .Where(d => d.Id == id) .AsEnumerable() .FirstOrDefault();​ 
-4
source

All Articles