2014-03-04 43 views
2

我是初學者,想了解更多關於Azure的表存儲,但我胡說,這個簡單的例子突破:無法保存引用類型

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Microsoft.WindowsAzure; 
using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Table; 

namespace AzureTablesTest 
{ 
    public class PersonName 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
    } 
    public class Person : TableEntity 
    { 
     public PersonName Name { get; set; } 
     public int Age { get; set; } 

     public Person() 
     { 
     } 

     public Person(string lastname, string firstname) 
     { 
      RowKey = firstname; 
      PartitionKey = lastname; 
      Name = new PersonName { FirstName = firstname, LastName = lastname }; 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Retrieve the storage account from the connection string. 
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true"); 

      // Create the table client. 
      CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 

      // Create the table if it doesn't exist. 
      CloudTable table = tableClient.GetTableReference("people"); 
      table.CreateIfNotExists(); 

      Person p = new Person("Foo", "Bar"); 

      TableOperation insertOperation = TableOperation.Insert(p); 

      // Execute the insert operation. 
      table.Execute(insertOperation); 

      TableOperation retrieveOperation = TableOperation.Retrieve<Person>("Foo", "Bar"); 

      // Execute the retrieve operation. 
      TableResult retrievedResult = table.Execute(retrieveOperation); 

      string firstname = ((Person)retrievedResult.Result).Name.FirstName; 
     } 
    } 
} 

它看起來像Azure中不能存儲引用類型。當我保存,然後檢索的人,名稱是檢索的人空,因此,我想從人的名稱屬性來獲取名字的時候得到一個空指針異常:

string firstname = ((Person)retrievedResult.Result).Name.FirstName; 

我沒有閱讀這是不可能的任何地方。我用這個作爲學習的入口:http://www.windowsazure.com/en-us/documentation/articles/storage-dotnet-how-to-use-table-storage-20/。通過閱讀「引進」能夠服務的網絡規模應用

  • 存儲的數據集,不需要複雜的連接,外鍵或存儲過程,可以非規範化快速訪問
  • 的結構化數據的

    1. TB的存儲
    2. 使用聚集索引
    3. 使用OData協議和LINQ查詢與WCF數據服務.NET庫
    在我眼裏

    訪問數據快速查詢數據,我的考試應該有可能。但我猜這是不可能的?

    +0

    你得到的錯誤是什麼? –

    +0

    被剔除者的姓名爲空。所以我得到一個空指針錯誤。在我看來,錯誤是我無法存儲參考類型。 – mslot

    +0

    嗯..我之前沒有使用過Azure庫,但是在我聽到您的Person對象不存在於表中時,當您嘗試檢索它時。我很懷疑在插入後立即檢索對象。 –

    回答

    0

    有,你可以在你的TableEntity使用,讓他們自動保存到表中的某些屬性類型。受支持的類型列表位於此網頁的末尾:http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx。您的PersonName類型是自定義類型,因此不受支持。

    您可以重寫TableEntity的ReadEntity和WriteEntity方法(http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.itableentity_methods.aspx)以自定義每個屬性的序列化/反序列化方式。然後,您可以執行一些操作,例如將PersonName中的每個子屬性保存到其自己的表屬性中。