将实体数据更新到 Azure 存储 table
Updating entity data to Azure storage table
我有一个场景,我 insert/update 数据到 Azure 存储 table 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);
}
您可以利用合并操作。请注意,如果您不想在更新之前读取实体,则应将 ETag 设置为“*”。
参考文献:
这里 InsertOrMerge
和 Merge
操作都可以。
我有一个场景,我 insert/update 数据到 Azure 存储 table 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);
}
您可以利用合并操作。请注意,如果您不想在更新之前读取实体,则应将 ETag 设置为“*”。
参考文献:
这里 InsertOrMerge
和 Merge
操作都可以。