2011-12-21 55 views
1

我試圖使用asure sdk 1.6(針對存儲模擬器)的新的upsert功能。Azure Table Storage sdk 1.6沒有插入的Upsert

但我只設法讓更新工作。當我嘗試插入一個新的rowkey時,我得到resource not found異常。

var context = new TableServiceContext(_cloudStorageAccount.TableEndpoint.ToString(), _cloudStorageAccount.Credentials) 
      { 
       MergeOption = MergeOption.NoTracking, 
       ResolveType = (unused) => typeof(SmartTableServiceEntity) 
      }; 
context.AttachTo(tableName, smartEntity, "*"); 
      context.UpdateObject(smartEntity); 
      context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate); 

如果我把AddObject它插入,但沒有更新。 由於新的SDK,我正在考慮能夠在一個操作中同時執行這兩個操作。

回答

3

我來到了這一點,似乎有效的解決方案上devstorage和實際存儲

var context = CreateNewContext(); 

      context.IgnoreResourceNotFoundException = true; 

      if (context.StorageCredentials.AccountName == "devstoreaccount1") 
      { 
       var entityCheck = context.CreateQuery<SmartTableServiceEntity>(tableName) 
        .Where(e => e.PartitionKey == partitionKey && e.RowKey == rowKey).FirstOrDefault(); 

       if (entityCheck == null) { 
        context.AddObject(tableName, smartEntity); 
       } 
       else { 
        context.Detach(entityCheck); 
        context.AttachTo(tableName, smartEntity, "*"); 
        context.UpdateObject(smartEntity); 
       } 
      } 
      else 
      { 
       context.AttachTo(tableName, smartEntity, null); 
       context.UpdateObject(smartEntity); 
      } 
      context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate); 

確實有人有更好的解決辦法? 注意到「*」和「null」之間的區別?

感謝您提前

+0

由於兩個原因,我不打算解決本地存儲的侷限性:(1)每月存儲每GB 0.14美元的定價和每10,000次存儲事務0.01美元的定價(這意味着我將使用真正的Azure帳戶); (2)大多數情況下,Windows Azure Tools/SDK的下一版本將具有本地存儲模擬器的該功能。 – astaykov 2011-12-21 20:12:10

+0

是的,這確實使sens。謝謝 :) – 2011-12-23 10:21:22

7

它只能對付真正的Azure存儲。開發存儲不支持Upsert操作。 此外,您必須將tableServiceContext的IgnoreResourceNotFoundException屬性設置爲true。

+0

謝謝你的幫助:) – 2011-12-21 17:50:41

+0

哇,會不會說什麼地方......花了幾個小時在這一個。 – AdamC 2012-06-06 22:59:49

+0

據說這裏:)在角落的新SDK可能會解決這個問題,但我100%確定。 – astaykov 2012-06-07 08:34:23

0

當使用真正的Azure的帳戶,以及設置IgnoreResourceNotFoundException爲true,它也是重要的傳遞null的eTag參數,或使用不接受的eTag超載值。否則,你會得到ResourceNotFound異常。