2012-05-22 27 views
3

我試圖控制序列化的Azure表實體作爲How do I retrieve multiple types of entities using a single query to Azure Table Storage?的後續行動,並遇到(對我來說)意外的行爲。爲什麼Azure表存儲在使用ResolveName時不會保留類型名稱?

當檢索項目的名稱始終是「<myaccountname>.Pets」不管我把它設置爲當我保存的實體。爲什麼不保存類型名?

根據MSDN:

DataServiceContext.ResolveType

獲取或設置用於覆蓋從接收實體時使用由客戶端庫的默認類型分辨率選項的功能開放數據協議(OData)服務。

DataServiceContext.ResolveName

獲取或設置一個功能覆蓋由客戶端庫使用的默認類型解析策略,當您發送實體數據服務。

this blog entry它不應該是這種情況。

下面是一個簡單的測試:

public class Pet : TableServiceEntity { } 
    public class Cat : Pet { } 
    public class Dog : Pet { } 


    public class Test 
    { 

     public void RunTest() 
     { 
      //this.Create(); 
      this.Read(); 
     } 

     public TableServiceContext GetTableServiceContext() 
     { 
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")); 
      CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 
      tableClient.CreateTableIfNotExist("Pets"); 

      TableServiceContext serviceContext = tableClient.GetDataServiceContext(); 

      serviceContext.ResolveName = (entityType) => 
      { 
       return entityType.FullName; 
      }; 

      serviceContext.ResolveType = (s) => 
      { 
       return Type.GetType(s); 
      }; 

      return serviceContext; 
     } 

     public void Create() 
     { 

      var serviceContext = this.GetTableServiceContext(); 

      // Create entries 
      var cat = new Cat() { PartitionKey = "cats", RowKey = "1" }; 
      serviceContext.AddObject("Pets", cat); 

      var dog = new Dog() { PartitionKey = "dogs", RowKey = "1" }; 
      serviceContext.AddObject("Pets", dog); 


      serviceContext.SaveChangesWithRetries(); 
     } 

     public void Read() 
     { 
      var serviceContext = this.GetTableServiceContext(); 

      var pets = serviceContext.CreateQuery<Pet>("Pets").AsTableServiceQuery<Pet>().ToArray(); 

      foreach (var pet in pets) 
      { 
       Console.WriteLine(pet.GetType()); 
      } 

     } 
    } 
+0

我正在深入探討這個問題,並會盡快添加更多信息,只要我有一些細節。 – AvkashChauhan

回答

1

一些挖掘和討論中,我發現類型名從未被蔚藍表看到後(這是無模式)。相反,所有公共屬性都是序列化的,只需將一個屬性的有效載荷發送到服務即可。

如果您需要確定您需要在有效負載中存儲/檢查屬性的類型。這可以利用讀/寫實體事件來完成。

您的目標是一次返回一個異構集合,或將其限制爲僅限一種類型。如果是後者,那麼如果可能的話,將類型存儲在主鍵,行鍵中或者在基於該字段的單獨字段和查詢中。

相關問題