Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update single property in azure table entity

Hi i am working on azure table "T1" and this table have list of entity set

Primarykey  RowKey   P1  P2
PP          R1       5   10
PP          R2       6   11

now suppose i want to update only P2 property and don't want to touch P1 property is this possibly to update single property in azure table entity. Remember i don't want to touch P1 property cos it continuously updating by other function

like image 302
sourabh devpura Avatar asked Aug 26 '16 09:08

sourabh devpura


1 Answers

The operation you would want to perform is Merge Entity. From the REST API documentation:

The Merge Entity operation updates an existing entity by updating the entity's properties. This operation does not replace the existing entity, as the Update Entity operation does.

Here's a sample code that you can use:

    static void MergeEntityExample()
    {
        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var client = account.CreateCloudTableClient();
        var table = client.GetTableReference("TableName");
        var entity = new DynamicTableEntity("PartitionKey", "RowKey");
        entity.ETag = "*";
        entity.Properties.Add("P2", new EntityProperty(12));
        var mergeOperation = TableOperation.Merge(entity);
        table.Execute(mergeOperation);
    }

The above code will only update "P2" property in the entity and will not touch other properties.

like image 108
Gaurav Mantri Avatar answered Oct 05 '22 23:10

Gaurav Mantri