Sam
Sam

Reputation: 30388

Add a column to existing entity in Azure Table Storage

I'm saving some transactional data in a table on Azure Table Storage. When I tried to "update" an existing entity in my table and add a new column to it, looks like it's not adding the column.

Here's an example: I'm saving time sheet entries in the table. When I first create the entity, there's a StartTime but no EndTime

PersonId -- TransactionId -- StartTime -- EndTime

So, when I first create the entity in the table, I end up with PersonId, TransactionId and StartTime columns. Later, I want to add the EndTime but looks like it's not adding it because in my object EndTime is a nullable property and when it's NULL, that column is not created.

Is there not a way to update an existing entity and add a column in the process? If not, I'll have to put some dummy date in the EndTime and store it during the initial creation of the entity so that the column is there and I can update it later.

I'd rather not store any dummy data though.

Upvotes: 0

Views: 5322

Answers (2)

Zhaoxing Lu
Zhaoxing Lu

Reputation: 6467

Azure Table Storage is a schema-less data base, which means entities within a table can have completely different columns actually.

In other words, you can create the entities without EndTime property if it's not needed, and added the property in the future by MergeEntity / UpdateEntity operations if EndTime becomes necessary later.

Upvotes: 2

Tom Sun
Tom Sun

Reputation: 24549

Is there not a way to update an existing entity and add a column in the process?

Yes, according to your case, we can do that with 2 ways: edit the transactional data model or using DynamicTableEntity

1.Edit the your transactional data model and set Endtime datetime and null value is accepted. Demo code as following,

 public class Transactional:TableEntity
    {
        public string PersonId { get; set; }
        public string TransactionId { get; set; }
        public DateTime StarTime { get; set; }
        public DateTime? EndTime { get; set; }

        public Transactional() { }

        // Define the PK and RK
        public Transactional(string persionId, string transactionId)
        {
            PartitionKey = persionId;
            RowKey = transactionId;
        }
    }  

If we don't assign the value to and try to insert to entity to the table, then there is no EndTime column. The following is the demo code.

 CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));
 var tableClient = storageAccount.CreateCloudTableClient();
 var table = tableClient.GetTableReference("tableName");
 table.CreateIfNotExists();
 var guid = Guid.NewGuid().ToString();
 Transactional transactional = new Transactional("tomtest", guid);
 transactional.StarTime =DateTime.UtcNow;
 TableOperation insertOrMergeOperation =   TableOperation.InsertOrMerge(transactional);         
 TableResult result = table.Execute(insertOrMergeOperation);    

How to update the entity

 // update entity
 TableOperation retrieveOperation = TableOperation.Retrieve<Transactional>("tomtest", "pk"); //PK, RK
 TableResult retrieveResult = table.Execute(retrieveOperation);
 Transactional updateEntity = retrieveResult.Result as Transactional;
 if (updateEntity != null) updateEntity.EndTime = DateTime.UtcNow;
 TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(updateEntity);
 // Execute the operation.
 TableResult resultinsertormerge = table.Execute(insertOrMergeOperation);  

2.We could use DynamicTableEntity to add a column in the process. The following is demo code.

  var entity = new DynamicTableEntity("tomtest", "pk"); //PK, RK
  entity.Properties.Add("EndTime", new EntityProperty(DateTime.UtcNow));      //properties want to add    
  var mergeOperation = TableOperation.InsertOrMerge(entity);
  table.Execute(mergeOperation);

Upvotes: 7

Related Questions