Question

I am a beginner, and want to learn more about Azure Table Storage, but I am babbled that this simple example breaks:

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;
        }
    }
}

It looks like Azure cannot store reference types. When I save, and then retrieve Person, Name is null on the person retrieved, and therefore I am getting a null pointer exception when trying to get the FirstName from the Name property of the person:

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

I have not read anywhere that this is not possible. I use this as a entry of learning: http://www.windowsazure.com/en-us/documentation/articles/storage-dotnet-how-to-use-table-storage-20/. By reading the "introduction"

  1. Storing TBs of structured data capable of serving web scale applications
  2. Storing datasets that don't require complex joins, foreign keys, or stored procedures and can be denormalized for fast access
  3. Quickly querying data using a clustered index
  4. Accessing data using the OData protocol and LINQ queries with WCF Data Service .NET Libraries

in my eyes, my example should be possible. But I guess it is not possible?

Was it helpful?

Solution

There are certain property types you can use in your TableEntity and have them saved to the table automatically. The list of supported types is at the end of this webpage: http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx. Your PersonName type is a custom type and therefore not supported.

You could override the ReadEntity and WriteEntity methods (http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.itableentity_methods.aspx) of your TableEntity to customize how each property is serialized/deserialized. Then you could do something like save each sub-property in PersonName to its own table property.

OTHER TIPS

Complex types are not supported by Table Storage.

See here for the list of allowable datatypes: http://msdn.microsoft.com/en-us/library/windowsazure/jj553018.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top