With azure tables, if I know an entity’s RowKey and PartitionKey (so I can retrieve that entity), how do I edit a particular property value on that entity?
This sounds like a pretty standard operation to do, but the normal way of doing it is something like:
public void UpdateEntity(ITableEntity entity)
{
TableOperation replaceOperation = TableOperation.Replace(entity);
table.Execute(replaceOperation);
}
i.e. a whole C# TableEntity object is given as a replacement, rather than an individual property name/value pair.
I want something more like:
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);
}
My understanding is that behind the scenes, the rows are stored as a set of key-value pairs corresponding to properties on that row, so updating a property’s value should be easy without having to define a whole C# class deriving from TableEntity to do so.
How would I do this?
Instead of “Replace” operation, do a “Merge” operation (http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.table.tableoperation.merge). Merge operation will ensure that only the property being modified is changed leaving all other properties unchanged.
A more complete example below. Here I first created an employee and then only changed the “MaritalStatus” property of that employee:
You can also try InsertOrMerge (http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.table.tableoperation.insertormerge.aspx) operation as well.