Ajay Bhasy
Ajay Bhasy

Reputation: 2000

TableStorage.ReplaceUpdateEntity

There is a method ReplaceUpdateEntity for the object of TableStorage which is used in AzureStorage section. I tried googling of what it might do or to get an example. Any brief description or a link pointing towards this is welcome.

TableStorageTestEntity tableStorageTestEntity = new TableStorageTestEntity()
        {
            TableId = Guid.NewGuid(),
            TablePurpose = "UpdateEntity"                
        };

        string partitionKey = "31072015";
        string rowKey = "0108201551";
        bool result = this.tableStorage.ReplaceUpdateEntity(this.TableName, partitionKey, rowKey, tableStorageTestEntity);
        Assert.IsTrue(result);

Actually I am running this test and the test fails. So I would like to understand about this method.

Upvotes: 0

Views: 53

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136146

The method you're looking for is InsertOrReplace. This operation will create an entity if it does not exist otherwise it will update the entity by replacing all of its attributes with the new values. Here's the sample code:

        var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
        var tableClient = account.CreateCloudTableClient();
        var table = tableClient.GetTableReference("Address");
        var entity = new DynamicTableEntity("pk", "rk");
        entity.Properties.Add("Attribute1", new EntityProperty("Attribute 1 Value"));
        TableOperation upsertOperation = TableOperation.InsertOrReplace(entity);
        table.Execute(upsertOperation);

Upvotes: 1

Related Questions