简体   繁体   English

将实体数据更新到 Azure 存储表

[英]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.我有一个场景,我将数据插入/更新到 Azure 存储表 2 值 MyValue 和 MyDate。

There are few scenarios where I have to update only 1 value MyValue and not MyDate.在少数情况下,我只需要更新 1 个值 MyValue 而不是 MyDate。

But When I do update operation, it updates bothe the values.但是当我进行更新操作时,它会更新两个值。 It changes myValue but makes MyDate to null.它更改 myValue 但使 MyDate 为 null。

Is there any operation in update where I can skip MyDate update and keep its value as it is?更新中是否有任何操作可以跳过 MyDate 更新并保持其值不变?

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

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.请注意,如果您不想在更新实体之前读取实体,则应将 ETag 设置为“*”。

References:参考资料:

  1. Merge Entity REST API合并实体 REST API
  2. TableOperation.Merge Method in .NET library .NET 库中的 TableOperation.Merge 方法

这里InsertOrMergeMerge操作都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM