Search code examples
azureazure-table-storageazure-tablequery

Is there a way to configure Azure Table updates to preserve future/unknown properties/columns?


Suppose I create a model

public class Foo :TableEntity {
   public int OriginalProperty {get;set;}
}

I then deploy a service that periodically updates the values of OriginalProperty with code similar to...

//use model-based query
var query = new TableQuery<Foo>().Where(…); 

//get the (one) result 
var row= (await table.ExecuteQueryAsync(query)).Single() 

//modify and write it back
row.OriginalProperty = some_new_value;
await table.ExecuteAsync(TableOperation.InsertOrReplace(row));

At some later time I decide I want to add a new property to Foo for use by a different service.

public class Foo :TableEntity {
   public int OriginalProperty {get;set;}
   public int NewProperty {get;set;}
}

I make this change locally and start updating a few records from my local machine without updating the original deployed service.

The behaviour I am seeing is that changes I make to NewProperty from my local machine are lost as soon as the deployed service updates the record. Of course this makes sense in some ways. The service is unaware that NewProperty has been added and has no reason to preserve it. However my understanding was that the TableEntity implementation was dictionary-based so I was hoping that it would 'ignore' (i.e. preserve) newly introduced columns rather than delete them.

Is there a way to configure the query/insertion to get the behaviour I want? I'm aware of DynamicTableEntity but it's unclear whether using this as a base class would result in a change of behaviour for model properties.

Just to be clear, I'm not suggesting that continually fiddling with the model or having multiple client models for the same table is a good habit to get into, but it's definitely useful to be able to occasionally add a column without worrying about redeploying every service that might touch the affected table.


Solution

  • You can use InsertOrMerge instead of InsertOrReplace.