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);