HelloWorld
HelloWorld

Reputation: 3739

How do I update an entity in Azure Table Storage?

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

Reading: https://learn.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?

Upvotes: 1

Views: 4176

Answers (2)

Gaurav Mantri
Gaurav Mantri

Reputation: 136146

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.

Upvotes: 5

HelloWorld
HelloWorld

Reputation: 3739

According to the HTTP API https://learn.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.

Upvotes: 2

Related Questions