简体   繁体   English

使用Entity Framework 4使用WebApi返回复杂对象

[英]Returning complex objects using WebApi using Entity Framework 4

I've built a REST service using WebApi and Entity Framework. 我已经使用WebApi和Entity Framework构建了REST服务。 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. 在我的应用程序中,我有2个项目-一个具有API功能,另一个具有将在Web项目中使用的模型类。

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: 举例来说,我有以下2类:

    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) 并且我想使用对存储库的以下调用来检索用户实体和所有用户成就的集合(假设我已经为两个对象中的每个对象都拥有了一个DbSet)

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 我只是认为必须有一种更好的方法...肯定会在webapi框架的开发中出现问题-我感觉好像在丢失一些基本知识

Thanks. 谢谢。

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. 您所缺少的,而且远不止一个的基本方面是,Web API框架的工作是使您能够使用HTTP将有效负载从A点传输到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. 对于简单的情况,Web API团队已尝试构建一些简单的有效负载构建工具,但是如果您要构建任何大小合适的应用程序,那么我建议您完全控制该过程。 Don't expect Web API to do that for you. 不要指望Web API为您做到这一点。

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; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM