How to get the Etag for an Item? #187
-
Is your feature request related to a problem? Please describe. Very briefly I tried updating the Describe the solution you'd like |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Is this how do you as a consumer read the |
Beta Was this translation helpful? Give feedback.
-
With the Cosmos SDK I would access the Etag from the returned result from the container read call ...
I wonder if there is an option for overrides of the |
Beta Was this translation helpful? Give feedback.
-
We aim to have dedicated documentation for this soon. There is a bit of explanation in the README see below. Optimistic Concurrency Control with EtagsThe default repository now supports etags and will pass them when Getting the new etagWhen creating a new object, if storing in memory, it is important to store the result from the create call to ensure you have the correct etag for future updated. For example your code should look something like this: TItem currentItem = new TItem(...);
currentItem = _repository.CreateAsync(currentItem); Sequential updatesWhen doing sequential updates to the same item it is important to use the result from the update method (when OptimizeBandwith is false) or refetch the updated data each time (when OptimizeBandwith is true) otherwise the etag value will not be updated. The following code shows what to do in each case: Optimize Bandwith OffTItem currentItem = _repository.CreateAsync(itemConfig);
currentItem = _repository.UpdateAsync(currentItem);
currentItem = _repository.UpdateAsync(currentItem); Optimize Bandwith OnTItem currentItem = _repository.CreateAsync(itemConfig);
_repository.UpdateAsync(currentItem);
currentItem = _repository.GetAsync(currentItem.Id);
_repository.UpdateAsync(currentItem);
currentItem = _repository.GetAsync(currentItem);
currentItem = _repository.UpdateAsync(currentItem); Catching mismatched etag errorsThe following code shows how to catch the error when the etags do not match. try
{
currentBankAccount = await repository.UpdateAsync(currentBankAccount);
Console.WriteLine($"Updated bank account: {currentBankAccount}.");
}
catch (CosmosException exception) when (exception.StatusCode == HttpStatusCode.PreconditionFailed)
{
Console.WriteLine("Failed to update balance as the etags did not match.");
} Ignoring the etagThe following code shows how to ignore the etag when doing an update. await repository.UpdateAsync(currentBankAccount, ignoreEtag: true); Passing the etag to a patch updateThe following code shows how to pass the etag when doing a update to specific properties. await repository.UpdateAsync(currentBankAccount.Id,
builder => builder.Replace(account => account.Balance, currentBankAccount.Balance - 250), etag: currentBankAccount.Etag); |
Beta Was this translation helpful? Give feedback.
-
Hi, @WestDiscGolf converted this to a discussion. Hope this is okay 😄 |
Beta Was this translation helpful? Give feedback.
We aim to have dedicated documentation for this soon.
There is a bit of explanation in the README see below.
Optimistic Concurrency Control with Etags
The default repository now supports etags and will pass them when
IItemWithEtag
is implemented correctly or the base classesEtagItem
orFullItem
are used. The etag check is enforced on all updates whenTItem
is of the correct type. It can however be bypassed by setting theignoreEtag
optional parameter in the relevant async methods. The InMemory repository also supports OCC with Etags. The OptimisticCurrencyControl sample shows these features.Getting the new etag
When creating a new object, if storing in memory, it is important to store t…