2013-05-13 32 views
0

我有一個用戶表,它具有一個名爲「PictureUri」的字符串類型的屬性。默認情況下,它爲空,然後我將它更新爲一些文本。後來,如果我嘗試再次更新爲空,它不會發生。更新成功,但字符串屬性不會更改爲空。我可以將相同的屬性更新爲其他文本,但不能爲空。無法將天藍色表存儲中的字符串類型實體更新爲空

蔚藍表存儲不允許我更新屬性爲null?

下面是一個示例代碼:

型號:

public class User 
    { 
     public string PictureUri{ get; set; } 
     public string Email { get; set; } 
     public string Username { get; set; } 
    } 

[System.Data.Services.Common.DataServiceKey(new string[] { "PartitionKey", "RowKey" })] 

    public class PersistedUser : User,ITableEntity 
    { 
     public string PartitionKey 
     { 
      get { return this.Email; } 
      set { this.Email = value; } 
     } 

     public string RowKey 
     { 
      get { return this.Username; } 
      set { this.Username = value; } 
     } 

     public DateTime Timestamp { get; set; } 

    } 

更新API

user.PictureUri = null; 
    tableServiceContext.AttachTo(TableNames.User, user); 
     tableServiceContext.Update(user); 
     tableServiceContext.SaveChanges(System.Data.Services.Client.SaveChangesOptions.ReplaceOnUpdate); 

     var tempUser = this.tableServiceContext.QueryableEntities.Where(u => u.RowKey.Equals(user.Username, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); 

     tableServiceContext.Detach(user); 

編輯:

使用SaveChangesOptions.ReplaceOnUpdate更新表中的值,但是當我嘗試要查詢它,它會返回舊數據。任何想法可能是什麼問題?

回答

2

嘗試使用以下SaveChanges()並提供ReplaceOnUpdate作爲SaveChangesOption。喜歡的東西:

 user.PictureUri = null; 
     tableServiceContext.AttachTo(TableNames.User, user); 
     tableServiceContext.Update(user); 
     tableServiceContext.SaveChanges(SaveChangesOption.ReplaceOnUpdate); 
     tableServiceContext.Detach(user); 

我認爲發生的事情是,默認保存選項是「合併」它不會改變未通過(即給定值爲null)值。

其他選項可能是將user.Picture1設置爲String.Empty而不是null

+0

感謝您的回覆。但隨着你的第一種方法,表得到正確更新,但當我試圖查詢它後立即,我得到舊數據。任何想法可能是什麼原因? – Bitsian 2013-05-14 07:39:30

+0

這應該不會發生,因爲Azure表存儲強烈一致。你能分享完整的代碼嗎? – 2013-05-14 08:05:50

+0

嘿,我已經添加了查詢記錄的代碼 – Bitsian 2013-05-14 09:06:18

相關問題