[英]Cannot save reference type
我是一個初學者,並且想了解有關Azure Table Storage的更多信息,但是我對這個簡單的示例不滿意而感到困惑:
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無法存儲引用類型。 當我保存然后檢索Person時,Name在檢索到的人員上為null,因此,當嘗試從該人員的Name屬性獲取FirstName時,出現了空指針異常:
string firstname = ((Person)retrievedResult.Result).Name.FirstName;
我沒有讀過這不可能的任何地方。 我將其用作學習的入口: http : //www.windowsazure.com/zh-cn/documentation/articles/storage-dotnet-how-to-use-table-storage-20/ 。 通過閱讀“介紹”
在我看來,我的榜樣應該是可能的。 但是我猜不可能嗎?
表存儲不支持復雜類型。
有關允許的數據類型的列表,請參見此處: http : //msdn.microsoft.com/zh-cn/library/windowsazure/jj553018.aspx
您可以在TableEntity中使用某些屬性類型,並將其自動保存到表中。 受支持的類型列表在此網頁的末尾: http : //msdn.microsoft.com/zh-cn/library/windowsazure/dd179338.aspx 。 您的PersonName類型是自定義類型,因此不受支持。
您可以重寫TableEntity的ReadEntity和WriteEntity方法( http://msdn.microsoft.com/zh-cn/library/microsoft.windowsazure.storage.table.itableentity_methods.aspx ),以自定義每個屬性的序列化/反序列化方式。 然后,您可以執行類似將PersonName中的每個子屬性保存到其自己的table屬性的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.