Azure / azure-cosmos-dotnet-v2

Contains samples and utilities relating to the Azure Cosmos DB .NET SDK
MIT License
577 stars 837 forks source link

Question: how to properly update an Document with an JsonPatchDocument #367

Closed MovGP0 closed 7 years ago

MovGP0 commented 7 years ago

I am wondering what the best way to patch an document is, when I want to preserve the ETag:

Version 1

JsonPatchDocument<SomeType> patch = ...;

// getting the document
Document document = Client.CreateDocumentQuery<Document>(uri, options)
                            .SingleOrDefault(doc => doc.Id == id);

// Problem: does not work
patch.ApplyTo(document);

var response = await Client.ReplaceDocumentAsync(uri, document, new RequestOptions());

Version 2

JsonPatchDocument<SomeType> patch = ...;

// getting the document
SomeType document = Client.CreateDocumentQuery<SomeType>(uri, options)
                            .AsQueryable()
                            .SingleOrDefault(doc => doc.Id == id);

// works
patch.ApplyTo(document);

// Problem: ETag missing; no check for concurrency issues 
var response = await Client.ReplaceDocumentAsync(uri, document, new RequestOptions());
kirankumarkolli commented 7 years ago

@MovGP0 optimistic concurrency requires RequestOptions.AccessCondition.

FYR Sample https://github.com/Azure/azure-documentdb-dotnet/blob/1b22cac61ef2b9eeae9ddadbd850e48f023abc54/samples/code-samples/DocumentManagement/Program.cs#L433

MovGP0 commented 7 years ago

@kirankumarkolli albeit your information is very useful, the question was a different one: how to keep the ETag when casting to a specific type.

The solution I have is to derive the POCO objects from the Microsoft.Azure.Documents.Resource class.