I have the following data structure:
[DataServiceEntity]
public class User
{
public string RowKey { get; set; }
public string PartitionKey { get; set; }
public DateTime Timestamp { get; set; }
public AddressValueObject Address { get; set; }
}
[DataServiceEntity]
public class AddressValueObject
{
public string City { get; set; }
public string Street { get; set; }
}
Now I’m creating a new instance of the User class and try to persist it into the Windows Azure Table Storage Emulator using following method:
void CreateAndSaveNewUser()
{
var userInstance = new User
{
RowKey = "SomeRowKey",
PartitionKey = "SomePartitionKey",
Address = new AddressValueObject
{
City = "SomeCity",
Street = "SomeStreet"
}
};
var tableServiceContext = CloudStorageAccount
.DevelopmentStorageAccount
.CreateCloudTableClient()
.GetDataServiceContext();
tableServiceContext.AddObject("UserTable", userInstance);
tableServiceContext.SaveChanges();
}
The problem is now that the AddressValueObject is not serialized correctly.
In the storage there is only one column called AddressValueObject which is emtpy.
No columns for AddressValueObject_City and AddressValueObject_Street are created.
What am I doing wrong? Am I missing some attribute?
Does Azure Table Storage even support referenced ValueObjects??
Thanks.
Simply you cannot do that. The Table Storage is a plain simple table and doesn’t supports complex types. Consequently relations or constraints are not supported.
As a workaround you may think to store the partition key and the row key of the related object stored in another table.
If you want to use relations, then the best choice is SQL Azure.