I have an Azure Table Storage that contains some data. I need to update one single property for all records in the table. I know the partition key and rowkey for each item. However might be so that I have new items in my CSV file.
What I need to do is:
I am trying to use InsertOrMerge but I got an Etag exception and how can I set up more properties if the item is not found and an insert will be required?
var storageAccount = CloudStorageAccount.Parse(connectionString);
var cloudTableClient = storageAccount.CreateCloudTableClient();
var ct = cloudTableClient.GetTableReference("mytable");
var item = new Item()
{
PartitionKey = "PARTITIONID",
RowKey = "ROWID",
Name = "DEMO",
};
var to = TableOperation.Merge(code);
var result = await ct.ExecuteAsync(to);
I also got etag exception when I use Merge
to operate a entity not exist in table.
System.ArgumentException: 'Merge requires an ETag (which may be the '*' wildcard).'
Your requirement can be achieved by Retrieve
and InsertOrMerge
.
Add two properties Email
and Address
to your Item
class.
public class Item: TableEntity
{
public Item(String PartitionKey, String RowKey, String Name, String Email=null, String Address=null)
{
this.RowKey = RowKey ;
this.PartitionKey = PartitionKey;
this.Name = Name;
this.Email = Email;
this.Address = Address;
}
public Item(){}
public String Name { get; set; }
public String Email { get; set; }
public String Address { get; set; }
}
Add if switch to tell which properties are to load.
TableOperation to = TableOperation.Retrieve<Item>("PK","RK");
TableResult tr = table.ExecuteAync(to).Result;
var item;
if (tr != null)
{
item = new Item
{
PartitionKey = "PARTITIONID",
RowKey = "ROWID",
Name = "DEMO",
}
}
else
{
item = new Item
{
PartitionKey = "PARTITIONID",
RowKey = "ROWID",
Name = "DEMO",
Email = "[email protected]",
Address = "Britain"
}
}
to = TableOperation.InsertOrMerge(item);
tr = await ct.ExecuteAysnc(to).Result;
. When you execute InsertOrMerge
,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With