【问题标题】:Updating entity data to Azure storage table将实体数据更新到 Azure 存储表
【发布时间】:2016-09-19 00:04:48
【问题描述】:

我有一个场景,我将数据插入/更新到 Azure 存储表 2 值 MyValue 和 MyDate。

在少数情况下,我只需要更新 1 个值 MyValue 而不是 MyDate。

但是当我进行更新操作时,它会更新两个值。它改变了 myValue 但使 MyDate 为空。

更新中是否有任何操作可以跳过 MyDate 更新并保持其值不变?

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

此代码插入或替换数据

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

【问题讨论】:

    标签: c# azure azure-table-storage


    【解决方案1】:

    您可以利用合并操作。请注意,如果您不想在更新之前读取实体,则应将 ETag 设置为“*”。

    参考资料:

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

    【讨论】:

    • 链接 #2 已损坏
    【解决方案2】:

    这里InsertOrMergeMerge都可以操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多