【发布时间】:2015-08-13 09:37:21
【问题描述】:
领域模型:
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; }
}
将课程扩展为包含 TeeSet 时,以下查询不包含 CourseRating 复杂类型。
GET /api/courses?$expand=TeeSets
public class CoursesController : ApiController
{
[Queryable]
public IQueryable<Course> Get()
{
return _uow.Courses.GetAll();
}
}
JSON 序列化结果不包含 MensRating 复杂类型(CourseRating):
[
{
"teeSets": [
{
"teeSetId": 1,
"courseId": 7
},
{
"teeSetId": 2,
"courseId": 7
}
],
"courseId": 7,
}
]
但是,对 DbContext 的快速测试会在 TeeSet 上返回 CourseRating 复杂类型,就像我期望的那样:
[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。
【问题讨论】:
标签: entity-framework asp.net-web-api odata