Given the name of the PartitionKey, RowKey, the name and value of the property, how do I change the value of an object property?

Using azure tables, if I know a RowKey and PartitionKey object (so that I can get this object), how can I edit a specific property value for this object?

This sounds like a pretty standard operation, but the usual way to do it is:

public void UpdateEntity(ITableEntity entity) { TableOperation replaceOperation = TableOperation.Replace(entity); table.Execute(replaceOperation); } 

i.e. the whole C # TableEntity object is defined as a replacement, not a separate pair of property names / values.

I want something more:

 public void UpdateEntityProperty<T>(string partitionKey, string rowKey, string propertyName, T newPropertyValue) { TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey); TableResult retrievedResult = table.Execute(retrieveOperation); TableEntity entity = (TableEntity)retrievedResult.Result; // This line, of course, doesn't compile. But you get the idea. entity.SetPropertyValue(propertyName, newPropertyValue); TableOperation replaceOperation = TableOperation.Replace(entity); table.Execute(replaceOperation); } 

I understand that behind the scenes, rows are stored as a set of key-value pairs corresponding to the properties in that row, so updating the property value should be simple without having to define an entire C # class derived from TableEntity before doing this.

How can I do it?

+4
source share
3 answers

Instead of the Replace operation, perform the Merge operation ( http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.table.tableoperation.merge ). The merge operation ensures that only the changed property is changed, leaving all other properties unchanged.

 public void UpdateEntityProperty<T>(string partitionKey, string rowKey, string propertyName, T newPropertyValue) { TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey); TableResult retrievedResult = table.Execute(retrieveOperation); TableEntity entity = (TableEntity)retrievedResult.Result; // This line, of course, doesn't compile. But you get the idea. entity.SetPropertyValue(propertyName, newPropertyValue); TableOperation mergeOperation = TableOperation.Merge(entity); table.Execute(mergeOperation); } 

A more complete example is below. Here I first created an employee, and then changed the โ€œFamilyStatusโ€ property of this employee:

 CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount; CloudTable table = storageAccount.CreateCloudTableClient().GetTableReference("Employee"); DynamicTableEntity entity = new DynamicTableEntity() { PartitionKey = "Employee", RowKey = "01", }; Dictionary<string, EntityProperty> properties = new Dictionary<string, EntityProperty>(); properties.Add("Name", new EntityProperty("John Smith")); properties.Add("DOB", new EntityProperty(new DateTime(1971, 1, 1))); properties.Add("MaritalStatus", new EntityProperty("Single")); entity.Properties = properties; TableOperation insertOperation = TableOperation.Insert(entity); table.Execute(insertOperation); DynamicTableEntity updatedEntity = new DynamicTableEntity() { PartitionKey = "Employee", RowKey = "01", ETag = "*", }; Dictionary<string, EntityProperty> newProperties = new Dictionary<string, EntityProperty>(); newProperties.Add("MaritalStatus", new EntityProperty("Married")); updatedEntity.Properties = newProperties; TableOperation mergeOperation = TableOperation.Merge(updatedEntity); table.Execute(mergeOperation); 

You can also try the InsertOrMerge operation ( http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.table.tableoperation.insertormerge.aspx .

+8
source

Just for completeness, here is what I ended up using, inspired by the Gaurar Mantry:

 public void UpdateEntityProperty(string partitionKey, string rowKey, string propertyName, string newPropertyValue) { var entity = new DynamicTableEntity(partitionKey, rowKey); var properties = new Dictionary<string, EntityProperty>(); properties.Add(propertyName, new EntityProperty(newPropertyValue)); var mergeOperation = TableOperation.Merge(entity); table.Execute(mergeOperation); } 
+9
source

Try the following:

 if (entity != null) { entity.propertyName = newPropertyValue; TableOperation updateOperation = TableOperation.Replace(entity); table.Execute(updateOperation); } 
0
source

All Articles