2013-02-04 46 views
16

我正在使用Azure表存儲來存儲數據。我很困惑什麼時候使用插入或替換以及插入或合併。我正在使用Azure SDK 1.7。插入或合併實體與插入或替換實體之間的區別

我對插入或替換的理解是,如果實體存在,用新實體替換先前實體的整個屬性。如果新實體未定義屬性或具有屬性值null,則在更新時將刪除該屬性。

儘管在插入或合併中,即使新實體未在新實體中定義新屬性,舊屬性也會保留。我的理解是正確的嗎?

梅亨德

回答

16

是的!你的理解是正確的。

您可以通過正確的connectionStringtableName運行下面的C#代碼測試:

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

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" })); 
} 

你應該在你的表中的單個實體具有以下屬性的結束