我使用 Azure 表存储来存储数据。我对何时使用插入或替换和插入或合并感到困惑。我使用的是 Azure SDK 1.7。
我的理解是,如果实体存在,插入或替换是将以前实体的整个属性替换为新实体,如果新实体没有定义属性或属性值为空,那么该属性将在更新时被删除。
而在插入或合并中,即使新实体没有在新实体中定义新的属性,旧的属性也会被保留。我的理解是否正确?
马赫德
是的!你的理解是正确的。
你可以通过运行下面的C#代码来测试它,正确的是 connectionString
和 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" }));
}
你应该最终在你的表中找到一个具有以下属性的单一实体。
{
PartitionKey = "partition",
RowKey = "row",
MyString = "randomString",
MySecondString = "randomSecondString"
}