1
votes

I'm confused on how to get my objects from azure tables when the object contains a list of table entity objects. Here is an example of what I'm trying to store

public class Class1 : TableEntity
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public List<Class2> Items { get; set; }
}

public class Class2 : TableEntity
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

Right now I'm trying to partition by user id and then each class 1 would have a row key of some id so I can get a list of class1 for the given user, or I can get single class1 objects by knowing the user and the id. I'm coming from a sql world and am brand new to table storage so I might be thinking about this whole thing incorrectly.

When I try and save Class1 to table storage everything saves except for the list. Whats the best way to store class 1 object? Is that even possible. I'm also thinking maybe I shouldn't be storing the list in the same table as the main object. I have scenarios where I'll need to edit individual items in the list, but if I store them all in the one table I'll have to pull out the entire object and find the item from the list I want to edit and put the whole object back.

I've been going through the getting started with azure table storage guide but nothing there seemed to make sense for the scenario I'm looking at here.

1

1 Answers

5
votes

List is not a supported table storage type neither any other collection, Ienumerable types in .net. You could convert your list property to Json string and then it would be ok. Of course you could convert the whole object to json string and write to table but that would be like using azure table storage like blob storage. You can also consider azure cosmos db with document db data model which is json. And you could write list and other property types just as you create a json doc and save it to azure cosmos db.

May be one additional comment I would make is that, it is actually possible to write and read complex types to azure table storage as long as your complex types do not have IEnumerable, ICollection type properties. So you could have an object A which may have properties of complex types B and C which may have their own properties etc.. The way to write and read these types of objects to table storage is to use the recently added TableEntity.Flatten and ConvertBack methods (> v8.0.0.0 of Azure Storage .NET SDK).

These api s will handle flattening your complex objects with nested complex properties and converting the original complex object back when read from azure table storage transparently. The caveat is they do not support IEnumerable and ICollection type properties. I still thought it would be good to mention this with regards to the topic of writing complex types to table storage.

Update: I have extended ObjectFlattenerRecomposer Nuget package (which was the original source code for TableEntity.Flatten / ConvertBack<T> api s in Azure Storage SDK) to support IEnumerable types as well. So the package works exactly as explained below flattening / recomposing complex objects but it also handles now IEnumerables.

You can try it here: https://www.nuget.org/packages/ObjectFlattenerRecomposer/