将实体数据更新到Azure存储表(Updating entity data to Azure storage table)

编程入门 行业动态 更新时间:2024-10-06 20:29:11
实体数据更新到Azure存储表(Updating entity data to Azure storage table)

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

有几种情况我只需要更新1个值MyValue而不是MyDate。

但是当我更新操作时,它会更新值。 它改变myValue但使MyDate为null。

在更新中是否有任何操作我可以跳过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); }

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

最满意答案

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

参考文献:

合并实体REST API .NET库中的TableOperation.Merge方法

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:

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

更多推荐

本文发布于:2023-07-27 08:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   新到   数据   Azure   Updating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!