Question

How do I use the ETag of Azure Table Storage Entity to update the entity if the ETag has not changed?

Example:

   var query = (from ent in cloudservicetable.CreateQuery<CloudServiceTableEntity>()
               where ent.PartitionKey == "test"
               && ent.Status == "creating"
               && ent.Counter> 0
               select ent).AsTableQuery();



    var candidates = query.OrderByDescending(s=>s.Counter).ToList();
    bool found = false;
    while (!found && candidates.Any())
    {
        //Find best candidate
        var candidate = candidates.First();

        //If we can decrement the count with out the entity have been changed
        //it is a acceptable candidate.
        candidate.Counter--;
        var opr = TableOperation.Merge(candidate);
       // cloudservicetable.ExecuteAsync(opr) 
       // How do I only do the merge if the etag have not changed?

       //TODO If changed remove/update the candidate list

    }
    // if found we have a candidate
Was it helpful?

Solution

For conditional merges, you don't have to do anything. Your code will throw an error (PreCondition failed - Http Status Code 412) if during the merge operation ETag doesn't match. So your code above will work perfectly fine.

For unconditional merges, you would need to manually set the ETag property of the entity to *.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top