Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentDB ReplaceDocument Fails

Tags:

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

"Errors":["The input content is invalid because the required properties - 'id; ' - are missing","The request payload is invalid. Ensure to provide a valid request payload."]

It's a blog post scenario, when a new comment is added, I get the document, add the comment and call ReplaceDocumentAsync. Here's 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);

Post 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?

like image 913
Hossein Avatar asked Apr 19 '15 03:04

Hossein


Video Answer


2 Answers

OK I figured it out.

Each Document in DocumentDB needs to have an "id" property. If a class does not have one, it will get assigned one and saved into the document. With DocumentDB being case sensitive, my "Id" was just another property and a separate "id" was added and assigned to the document.

I fixed the issue by deleting and recreating all my documents with the following attribute for Id:

[JsonProperty(PropertyName = "id")]
public Guid Id { get; set; }
like image 97
Hossein Avatar answered Oct 29 '22 11:10

Hossein


alternatively you can tell Cosmos to use camelCasingWhichIsStandardForJson

new CosmosClient(
  connectionstring,
  new CosmosClientOptions(){
    SerializerOptions = new CosmosSerializationOptions(){
      PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
    }

  }
)
like image 22
Chris DaMour Avatar answered Oct 29 '22 10:10

Chris DaMour