[英]Azure Table Storage InsertOrMerge
I have an Azure Table Storage that contains some data.我有一个包含一些数据的 Azure 表存储。 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.
但是可能是这样我的 CSV 文件中有新项目。
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?我正在尝试使用 InsertOrMerge,但出现 Etag 异常,如果未找到该项目并且需要插入,我该如何设置更多属性?
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.当我使用
Merge
操作表中不存在的实体时,我也遇到了 etag 异常。
System.ArgumentException: 'Merge requires an ETag (which may be the '*' wildcard).'
Your requirement can be achieved by Retrieve
and InsertOrMerge
.您的要求可以通过
Retrieve
和InsertOrMerge
来实现。
Add two properties Email
and Address
to your Item
class.将两个属性
Email
和Address
添加到您的Item
类。
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.添加 if 开关以告知要加载哪些属性。
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 = "test@example.com",
Address = "Britain"
}
}
to = TableOperation.InsertOrMerge(item);
tr = await ct.ExecuteAysnc(to).Result;
. . When you execute
InsertOrMerge
,当您执行
InsertOrMerge
,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.