Kurkula
Kurkula

Reputation: 6762

Updating entity data to Azure storage table

I have a scenario where I insert/update data to Azure storage table 2 values MyValue and MyDate.

There are few scenarios where I have to update only 1 value MyValue and not MyDate.

But When I do update operation, it updates bothe the values. It changes myValue but makes MyDate to null.

Is there any operation in update where I can skip MyDate update and keep its value as it is?

public class MyEntity : TableEntity
{
public MyEntity(string partitionKey, string rowKey) : 
 base(partitionKey, rowKey)
 {
 }
public string MyValue { get; set; }
public DateTime MyTime { get; set; }
}

This code insert or replaces data

   var entity = new MyEntity(partitionKey, rowKey)
     {
        MyValue = "test my value",
        MyTime = DateTime.Now();
     };

    AddEntity(entity);



     public void AddEntity(MyEntity entity)
     {
     CloudTable table =     _tableClient.GetTableReference("myAzureStorageTableName");
 TableOperation insertOp = TableOperation.InsertOrReplace(entity);
 table.Execute(insertOp);                         
      }

Upvotes: 2

Views: 9881

Answers (2)

Zhaoxing Lu
Zhaoxing Lu

Reputation: 6467

You can leverage Merge operation. Please note that you should set ETag to "*" if you don't want to read the entity before updating it.

References:

  1. Merge Entity REST API
  2. TableOperation.Merge Method in .NET library

Upvotes: 2

Alex Chen-WX
Alex Chen-WX

Reputation: 531

Here Both InsertOrMerge and Merge operation are ok.

Upvotes: 3

Related Questions