简体   繁体   English

Azure 表存储 InsertOrMerge

[英]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:我需要做的是:

  • if an item is found in the table storage based on ParitionKey and RowKey I want to update one single property: Name.如果在基于 ParitionKey 和 RowKey 的表存储中找到一项,我想更新一个属性:名称。
  • if an item is not found in the table it must be inserted but I have more properties that need to be filled: Name, Email, Address如果在表中找不到某个项目,则必须插入它,但我有更多的属性需要填写:姓名、电子邮件、地址

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 .您的要求可以通过RetrieveInsertOrMerge来实现。

Add two properties Email and Address to your Item class.将两个属性EmailAddress添加到您的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

  • If the item exists, its content(Name) will be updated by your new item.如果该项目存在,其内容(名称)将被您的新项目更新。
  • If it doesn't exist, it will be inserted as expected.如果它不存在,它将按预期插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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