如果 ETag 未更改,如何使用 Azure 表存储实体的 ETag 更新实体?
例:
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
对于条件合并,您无需执行任何操作。如果在合并操作期间ETag
不匹配,您的代码将引发错误(PreCondition
失败 - Http Status Code 412
)。所以你上面的代码将完美地工作。
对于无条件合并,您需要手动将实体的 ETag
属性设置为 *
。