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

发布于 2024-12-21 10:39:25 字数 1133 浏览 0 评论 0原文

我使用 WebApi 和实体框架构建了 REST 服务。在我的应用程序中,我有 2 个项目 - 一个具有 API 功能,另一个具有我将在 Web 项目中使用的模型类。

我遇到的问题是我似乎无法呈现任何实体的子集合。举例来说,我有以下 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; }
    }

并且我想使用以下对我的存储库的调用来检索一个 User 实体和所有用户成就的集合(假设我已经为这 2 个对象中的每一个对象都有一个 DbSet) )

var user = dbContext.Users
                .Include(u=>u.Achievements)
                .Where(u=>u.UserId == 1)
                .First();

我已经运行了这段代码,并通过包含上述语句的方法进行了调试,它正确地检索了我需要的所有信息,但是,在此之后,数据不会呈现给浏览器,而是返回内容长度零。

我已经阅读了大量有关此事的信息,似乎有人建议创建一个自定义序列化器来处理复杂的外部实体。我只是认为必须有更好的方法......这肯定是 webapi 框架开发中的一个问题 - 我觉得我好像错过了一些基本的东西,

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2024-12-28 10:39:25

您所缺少的基本部分(而且您并不孤单)是 Web API 框架的工作是使您能够使用 HTTP 将有效负载从 A 点传输到 B 点。

这些有效负载的构造方式是完全独立的问题,应将其视为应用程序的独特部分。 Web API 团队尝试构建一些简单的有效负载构建工具,以应对简单的情况,但如果您正在尝试构建任何大小合适的应用程序,那么我建议您完全控制该过程。不要指望 Web API 可以为您做到这一点。

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.

十秒萌定你 2024-12-28 10:39:25
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; }
    }
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; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文