2

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.

Matthew Merryfull
  • 1,466
  • 18
  • 32

2 Answers2

4

The fundamental piece that you are missing, and you are far from alone, is that the job of the Web API framework is to enable you to use HTTP to transfer payloads from point A to point B.

How those payloads are constructed is a completely separate issue and should be treated as a distinct part of your app. The Web API team have attempted to build in some simple payload construction tools, for the easy cases, but if you are trying to build any decent sized app then I suggest you take full control over that process. Don't expect Web API to do that for you.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
0
let me know if its a serialization issue.

[DataContract]  
public class User
{
    [DataMember]
    public int UserId { get; set; }

  [DataMember]
        public string Name { get; set; }

  [DataMember]
        public string FirstName { get; set; }

  [DataMember]
        public string LastName { get; set; }

  [DataMember]
        public virtual Collection<Achievement> Achievements { get; set; }
    }

   [DataContract]  
    public class Achievement
    {

  [DataMember]
        public int AchievementId { get; set; }

  [DataMember]
        public string Achievement { get; set; }

  [DataMember]
        public string Value { get; set; }

  [DataMember]
        public User User { get; set; }
    }
Andrew
  • 1,696
  • 2
  • 16
  • 20