天蓝色表存储中的事务

ren*_*ren 4 azure azure-table-storage

假设我有:

using (TransactionScope scope = new TransactionScope()) 
{
    if (IndexExists(index.RowKey))
        DeleteIndex(index.RowKey); //deletes using TableOperation.Delete

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    CloudTable table = tableClient.GetTableReference(Const.IndexTable);

    TableOperation insertOperation = TableOperation.Insert(index);
    table.Execute(insertOperation);   
}
Run Code Online (Sandbox Code Playgroud)

我想要的是:如果插入失败,删除应该撤消.这是正确的交易方式吗?一切都发生在同一个分区/表中.还有什么是交易的其他限制,我读过某个地方,交易中可以存储不超过4 Mb,这仍然是正确的吗?

Gau*_*tri 9

假设需要执行操作的所有实体具有相同的PartitionKey,您可以使用Entity Group TransactionWindows Azure表存储中提供的功能.它正是如此.如果对事务中的实体的操作失败,则回滚整个事务.但是,您似乎正在删除实体并再次创建相同的实体.该场景在实体批处理事务中不起作用,因为实体在事务中只能出现一次,并且只能对实体执行一个操作.看起来你感兴趣的是替换一个实体.在这种情况下,您可以直接使用InsertOrReplace()功能.