假设我们有2种类型,通过EF 4映射到数据库.
Schedule 1.....1 Visit
此外,我们有第三个自定义视图类型
public class ScheduleView
{
public Schedule Schedule { get; set; }
public Visit Visit { get; set; }
}
所以我们可以编写连接查询
var query = Context.Schedule.Join(Context.Visit
,/*Schedule join key definition*/,/*Visit join key definition*/,
(scheduleView, visit) => new ScheduleView {Schedule = scheduleView, Visit = visit})
问题是我还需要加载Visit类型的Patient属性.但是当我写作
query = (query as ObjectQuery<ScheduleView>).Include("Visit.Patient");
我收到运行时错误
Unable to cast the type
‘System.Linq.IQueryable1' to type
1′.
'System.Data.Objects.ObjectQuery
LINQ to Entities only supports casting
Entity Data Model primitive types.
所以,问题是 – 如何强制查询在我的自定义类型中包含某些内容?
最佳答案 最后,开发了一些丑陋的解决方法 – 引入了自定义类型的新成员并明确查询它.
public class ScheduleView
{
public Schedule Schedule { get; set; }
public Visit Visit { get; set; }
**public Patient Patient{ get; set; }**
}
var query = Context.Schedule.Join(Context.Visit
,/*Schedule join key definition*/,/*Visit join key definition*/,
(scheduleView, visit) => new ScheduleView
{Schedule = scheduleView, Visit = visit, **Patient = visit.Patient**})
现在我在我的自定义类型中正确加载了患者.很有趣,但是当我在介绍ScheduleView.Patient后调查ScheduleView.Visiting.Patient时,我发现它也被加载了.
在这种情况下无法获得EF逻辑.并且dunno如何强制加载ScheduleView.Visiting.Patient而不必加载无用的ScheduleView.Patient 🙁