I've built a REST service using WebApi and Entity Framework. In my application I've got 2 projects - one with the API functionality and the other with the model classes that I will use in my web project.
The problem I'm having is that I cannot seem to render child collections for any of the entities. Say for example that I have the following 2 classes:
public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual Collection<Achievement> Achievements { get; set; }
}
public class Achievement
{
public int AchievementId { get; set; }
public string Achievement { get; set; }
public string Value { get; set; }
public User User { get; set; }
}
and I wanted to retrieve a User entity and a collection of all the User's achievements using the following call to my repository (assuming that I have a DbSet for each of the 2 objects in place already)
var user = dbContext.Users
.Include(u=>u.Achievements)
.Where(u=>u.UserId == 1)
.First();
I've run this code and debugged through the method containing the statement above and it correctly retrieves all of the information that I require, however, after this point the data is not rendered to the browser, instead a content length zero is returned.
I've read a lot of extensive information on the matter and it seems like there are suggestions to create a custom Serializer to handle the complex foreign entities. I just think that there's got to be a better way...surely this would have been an issue in the development of the webapi framework - I feel as though I'm missing something fundamental
Thanks.