Search code examples
entity-frameworkazurelinq-to-entitiesazure-mobile-services

How to get the referenced object details


I have two models

public class Movie : EntityData
{
    public string Title { get; set; }
    public string Description { get; set; }        
}

and

public class Category : EntityData
{
    public string Name { get; set; }
    public List<Movie> Movies { get; set; }
}

Using the codefirst migrations, it created two tables with foreign key relationship. It added a column name MovieCategoryID in the Movies table.

I created a TableController for Category and want to retrieve all the categories.

public class CategoryController : TableController<Category>
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        MobileServiceContext context = new MobileServiceContext();
        DomainManager = new EntityDomainManager<Video>(context, Request);
    }

    public IQueryable<Category> GetAllCategories()
    {
        return Query();
    }
}

When I try to get the categories as below from the client

   this.client = new MobileServiceClient(Constants.ApplicationURL);
   this.categoryTable = client.GetTable<Category>();
   IEnumerable<Category> categories = await categoryTable.ToEnumerableAsync();

The `Movies' property of all the categories is null. What is the better way to tell the Azure table controller to fetch all the related 'Movies'?

Please let me know if you need any clarification!


Solution

  • Azure Mobile Apps does not support relationships.

    You can use an automatic $expand query on the backend. For information on this, see https://shellmonger.com/2016/05/27/30-days-of-zumo-v2-azure-mobile-apps-day-26-relationship-advice/

    You can read more about the data story for Azure Mobile Apps at http://aka.ms/zumobook - chapter 3.