Tom Glenn
Tom Glenn

Reputation: 2042

Replacing Document in DocumentDB Not Saving Changes

I have a document that inherits from the Azure Microsoft.Azure.Documents.Document class, and this is the structure of my properties.

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("establishment")]
public string Establishment { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

I can save documents fine, however I am running into an issue when trying to update a document and replace it.

For example, if I were to do the following, the document's description never gets updated.

var project = documentRepository.FindById<ProjectDocument>("7fc2899e-4c7f-4c09-9f60-4ca28caca750");
project.Description = "Foo";
await documentRepository.Save(project);

However if I do this, then it does.

var project = documentRepository.FindById<ProjectDocument>("7fc2899e-4c7f-4c09-9f60-4ca28caca750");
project.SetPropertyValue("description", "Foo");
await documentRepository.Save(project);

Obviously I don't want to have to do this as it seems unneccessary.

It is worth noting that I am using my own library called Documental to use a repository pattern, but the underlying code is just calling the Azure DocumentDB library, i am having the same issues when directly using the DocumentDB library.

UPDATE: The runtime view of the object contains the correct value for the changed field as expected, but the Dynamic View of the object always contains its original value unless I use the SetPropertyValue method.

Upvotes: 3

Views: 1569

Answers (2)

Karthikeyan VK
Karthikeyan VK

Reputation: 6026

In your code, example You can use **upsert** that is available in document db API, which does all the dirty work for you and it replaces the document. you find details here

Upvotes: 0

Rajesh Nagpal
Rajesh Nagpal

Reputation: 1118

As I mentioned in the associated Github post that there seems to be a bug when extending classes from Document. Please extend from Resource as a base class and everything should work fine. You would then need to use the other overloads for ReplaceDocumentAsync that accept the docLink and the updated object.

Let me know if that works for you.

Upvotes: 3

Related Questions