skip to Main Content

I’m running a multi-stage process which needs to update a given Entity (which already exists before the process starts) several times. There’s some time between subsequent updates, during which other parties are expected to observe the intermediary states of the Entity. And they’re not supposed to change the Entity during the process, but I can’t assume that they really won’t.

So in the beginning I retrieve the existing Entity (tableClient.GetEntityIfExistsAsync<MyEntity>), and I note its ETag.

Then, the first time I change the Entity, I use the ETag value from before to ensure I’m still working with the original Entity and that no one has changed it in between: tableClient.UpdateEntityAsync(myUpdatedEntity, originalETag).

So far so good, but now what do I do when I want to change the Entity again? I don’t have its updated ETag, and if I try and retrieve it again so that I get the new ETag I run the risk of it having been changed between the update and the query.

So is there any way to (atomically) obtain an Entity’s (updated) ETag when updating it?

2

Answers


  1. You should be able to get the latest value of ETag in response headers.

    Please try something like the following:

    var result = await tableClient.UpdateEntityAsync(myUpdatedEntity, originalETag);
    var headers = result.Headers;
    var etag = headers.ETag;
    

    Reference: https://learn.microsoft.com/en-us/dotnet/api/azure.response?view=azure-dotnet.

    Login or Signup to reply.
  2. You can make use of the UpdateEntityAsync method provided by the TableClient class. After updating the entity, the method will return the updated entity along with the new ETag value.

    var tableClient = new TableClient(connectionString, tableName);
    TableEntity entity = await tableClient.GetEntityAsync<TableEntity> 
                                (partitionKey, rowKey);
    entity.SomeProperty = "New Value";
    Response<TableEntity> response = await 
                                     tableClient.UpdateEntityAsync(entity);
    var updatedETag = response.Value.ETag;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search