Add or replace entity in Azure Table Storage

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

    Insert/Merge or Update was added to the API in September 2011. Here is an example using the Storage API 2.0 which is easier to understand then the way it is done in the 1.7 api and earlier.

    public void InsertOrReplace(ITableEntity entity)
        {
            retryPolicy.ExecuteAction(
                () =>
                {
                    try
                    {
                        TableOperation operation = TableOperation.InsertOrReplace(entity);
                        cloudTable.Execute(operation);
                    }
                    catch (StorageException e)
                    {
                        string message = "InsertOrReplace entity failed.";
    
                        if (e.RequestInformation.HttpStatusCode == 404)
                        {
                            message += " Make sure the table is created.";
                        }
    
                        // do something with message
                    }
                });
        }
    

提交回复
热议问题