Difference between Insert Or Merge Entity and Insert Or Replace Entity

霸气de小男生 提交于 2020-01-28 10:25:20

问题


I'm using Azure table storage for storing the data. I'm getting confused on when to use insert-or-replace and insert-or-merge. I'm using Azure SDK 1.7.

My understanding on insert or replace is replacing entire property of previous entity with new entity if the entity exists. if the new entity doesn't define property or having property value null then that property will be removed on updating.

Whereas in insert-or-merge, old properties will be retained even if the new entity didn't define new properties in new entity. Is my understanding is correct?

Mahender


回答1:


Yes! Your understanding is correct.

You can test it by running following C# code with a correct connectionString and tableName:

class MyEntity : TableEntity
{
  public string MyString { get; set; }
}

class MySecondEntity : TableEntity
{
  public string MySecondString { get; set; }
}

public void MergeTest()
{
  CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
  CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
  CloudTable table = tableClient.GetTableReference(tableName);
  table.CreateIfNotExists();
  // Insert an entity
  table.Execute(TableOperation.Insert(new MyEntity()
  { PartitionKey = "partition", RowKey = "row", MyString = "randomString" }));
  // Merge with a different class
  table.Execute(TableOperation.InsertOrMerge(new MySecondEntity()
  { PartitionKey = "partition", RowKey = "row", MySecondString = "randomSecondString" }));
}

You should end up with a single entity in your table with the following properties:

{
  PartitionKey = "partition",
  RowKey = "row",
  MyString = "randomString",
  MySecondString = "randomSecondString"
}


来源:https://stackoverflow.com/questions/14685907/difference-between-insert-or-merge-entity-and-insert-or-replace-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!