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?
Just for completeness, here's what I ended up using, inspired by Gaurav Mantri's answer:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With