Web API OData $ expand不返回复杂类型 [英] Web API OData $expand doesn't return complex types

查看:154
本文介绍了Web API OData $ expand不返回复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

域名模型:

public class Course
{
   public int CourseId { get; set; }
   public virtual ICollection<TeeSet> TeeSets { get; set; }
}
public class TeeSet
{
    public int TeeSetId { get; set; }
    public int CourseId { get; set; }
    public CourseRating MensRating { get; set; }
}

当课程扩展到以下查询时,不包括CourseRating复杂类型包括TeeSets。

GET / api / courses?$ expand = TeeSets

The following query does not include the CourseRating complex type when Courses are expanded to include TeeSets.
GET /api/courses?$expand=TeeSets

public class CoursesController : ApiController
{
    [Queryable]
    public IQueryable<Course> Get()
    {
        return _uow.Courses.GetAll();
    }
}

JSON序列化结果不包括MensRating复杂类型(CourseRating):

The JSON serialized result does not include the MensRating complex type (CourseRating):

[
  {
"teeSets": [
  {
    "teeSetId": 1,
    "courseId": 7
  },
  {
    "teeSetId": 2,
    "courseId": 7
  }
   ],
   "courseId": 7,
  }
]

然而,对DbContext的快速测试返回TeeSets上的CourseRating复杂类型,就像我预期的那样:

However, a quick test against the DbContext returns the CourseRating complex type on TeeSets like I would expect:

[TestMethod]
public void Get_Course_With_TeeSets()
{
    using (CoursesContext ctx = new CoursesContext())
    {
        var courses = ctx.Courses.Where(x => x.CourseId == 7).Include(x => x.TeeSets).FirstOrDefault();
    }
}

使用实体框架6和Web API 2。 p>

Entity Framework 6 and Web API 2 used.

推荐答案

添加 AutoExpand MensRating 属性可以使这个工作。

Add AutoExpand on MensRating Property can make this work.

这篇关于Web API OData $ expand不返回复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆