Add or replace entity in Azure Table Storage

后端 未结 5 479
攒了一身酷
攒了一身酷 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:26

    As you've found, you can't just add another item that has the same row key and partition key, so you will need to run a query to check to see if the item already exists. In situations like this I find it helpful to look at the Azure REST API documentation to see what is available to the storage client library. You'll see that there are separate methods for inserting and updating. The ReplaceOnUpdate only has an effect when you're updating, not inserting.

    While you could delete the existing item and then add the new one, you could just update the existing one (saving you one round trip to storage). Your code might look something like this:

    var existsQuery = from e
                        in tableServiceContext.CreateQuery(TableName)
                        where
                        e.PartitionKey == objectToUpsert.PartitionKey
                        && e.RowKey == objectToUpsert.RowKey
                        select e;
    
    MyEntity existingObject = existsQuery.FirstOrDefault();
    
    if (existingObject == null)
    {
        tableServiceContext.AddObject(TableName, objectToUpsert);
    }
    else
    {
        existingObject.Property1 = objectToUpsert.Property1;
        existingObject.Property2 = objectToUpsert.Property2;
    
        tableServiceContext.UpdateObject(existingObject);
    }
    
    tableServiceContext.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);
    

    EDIT: While correct at the time of writing, with the September 2011 update Microsoft have updated the Azure table API to include two upsert commands, Insert or Replace Entity and Insert or Merge Entity

提交回复
热议问题