Reputation: 227
I have a delete operation that tries to delete various entries from an Azure storage table. Same table, same partition key.
private async Task RemoveNodesAsyc(string rowKey, string partitionId)
{
var result = this.GetNodes(this.table, rowKey, partitionId);
foreach (var node in result)
{
TableOperation deleteOperation = TableOperation.Delete(node);
TableResult resultDelete = await table.ExecuteAsync(deleteOperation);
}
}
I would like that in case I am deleting 50 rows, and the row number 47 fails, the changes were rolled back, so that I do not end up with 47 rows deleted and 3 rows hanging there in the ether.
Is there any way to do the above code in a transaction-like mode? I have been googling and I found for example this answer here, but Microsoft's documentation about group entity transactions talks about how to use the Rest API only. Is that functionality even available in C#? How can I say in plain C# something like start transaction, do operations, and commit/rollback by the end?
Upvotes: 1
Views: 550
Reputation: 136196
Is there anyway to do the above code in a transaction like mode?
You should be able to do it using TableBatchOperation
and ExecuteBatchAsync
.
Your code would be something like (untested code though):
private async Task RemoveNodesAsyc(string rowKey, string partitionId)
{
var result = this.GetNodes(this.table, rowKey, partitionId);
TableBatchOperation batch = new TableBatchOperation();
foreach (var node in result)
{
batch.Delete(node);
}
TableBatchResult result = await this.table.ExecuteBatchAsync(batch);
}
Upvotes: 1