简体   繁体   中英

How do I update an entity in Azure Table Storage?

How do I update a single entity in an Azure table?

Reading: https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.table.tableoperation.merge?view=azure-dotnet

It simply states that it merges the entity.

How does it merge?

Which properties are overwritten, and which are not?

Will entity properties whose values are null not be updated?

Yes, no, maybe?

To understand how Merge operation works, consider this example.

Let's say you have an entity like the following:

PartitionKey: "PK"

RowKey: "RK"

Attribute1: "Value 1"

Attribute2: "Value 2"

Now you want to update that entity. What you do is change the value of Attribute1 and add a new attribute Attribute3 .

PartitionKey: "PK"

RowKey: "RK"

Attribute1: "Value 1 (Updated)"

Attribute3: "Value 3"

Once you update the entity using Merge , the resulting entity would be:

PartitionKey: "PK"

RowKey: "RK"

Attribute1: "Value 1 (Updated)"

Attribute2: "Value 2"

Attribute3: "Value 3"

To summarize Merge operation:

  • Any attribute that is present in both original and updated entity will be updated.
  • Any attribute that is present in the original entity but not in updated entity will not be changed.
  • Any attribute that is not present in the original entity but present in updated entity will be added.

Please note that there's Replace Entity operation as well which replaces the original entity with the updated entity. So with the same example, if you update an entity using Replace Entity operation, resulting entity would be:

PartitionKey: "PK"

RowKey: "RK"

Attribute1: "Value 1 (Updated)"

Attribute3: "Value 3"

To summarize Replace operation:

  • Any attribute that is present in both original and updated entity will be updated.
  • Any attribute that is present in original entity but not in updated entity will be removed.
  • Any attribute that is not present in original entity but present in updated entity will be added.

According to the HTTP API https://docs.microsoft.com/en-us/rest/api/storageservices/merge-entity :

The Table service does not persist null values for properties. Specifying a property with a null value is equivalent to omitting that property in the request. Only properties with non-null values will be updated by the Merge Entity operation.

Assuming this goes for the C# SDK as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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