Add or replace entity in Azure Table Storage

后端 未结 5 476
攒了一身酷
攒了一身酷 2021-01-07 23:41

I\'m working with Windows Azure Table Storage and have a simple requirement: add a new row, overwriting any existing row with that PartitionKey/RowKey. However, saving the c

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-08 00:41

    in my case it was not allowed to remove it first, thus I do it like this, this will result in one transaction to server which will first remove existing object and than add new one, removing need to copy property values

           var existing = from e in _ServiceContext.AgentTable
                           where e.PartitionKey == item.PartitionKey
                                 && e.RowKey == item.RowKey
                           select e;
    
            _ServiceContext.IgnoreResourceNotFoundException = true;
            var existingObject = existing.FirstOrDefault();
    
            if (existingObject != null)
            {
                _ServiceContext.DeleteObject(existingObject);
            }
    
            _ServiceContext.AddObject(AgentConfigTableServiceContext.AgetnConfigTableName, item);
    
            _ServiceContext.SaveChangesWithRetries();
            _ServiceContext.IgnoreResourceNotFoundException = false;
    

提交回复
热议问题