实体框架:如何做正确的"包括"在自定义类型 [英] Entity Framework: how to do correct "Include" on custom type
问题描述
假设我们有2种,通过EF 4映射到数据库。
Suppose we have 2 types, mapped to a Database via EF 4.
Schedule 1.....1 Visit
此外,我们还有第三个自定义视图类型
Also, we have third custom view type
public class ScheduleView
{
public Schedule Schedule { get; set; }
public Visit Visit { get; set; }
}
所以,我们可以这样写连接查询
So we can write the join query
var query = Context.Schedule.Join(Context.Visit
,/*Schedule join key definition*/,/*Visit join key definition*/,
(scheduleView, visit) => new ScheduleView {Schedule = scheduleView, Visit = visit})
问题是,我也需要加载患者
访问的财产
键入。但是,当我写
The problem is that I need to load also Patient
property of Visit
type. But when I write
query = (query as ObjectQuery<ScheduleView>).Include("Visit.Patient");
我收到运行时错误
I receive a runtime error
无法转换类型
'System.Linq.IQueryable 1键入
1。
'System.Data.Objects.ObjectQuery
LINQ到实体仅支持铸造
实体数据模型的基本类型
Unable to cast the type 'System.Linq.IQueryable
1' to type 'System.Data.Objects.ObjectQuery
1'. LINQ to Entities only supports casting Entity Data Model primitive types.
所以,问题是 - 怎么样?强制查询,以包括我的自定义类型中的东西。
So, the question is - how to force query to include something within my custom type?
推荐答案
最后,开发了一些丑陋的解决方法 - 在自定义类型引入了新的成员, 。显式查询,它
Finally, developed some ugly workaround - introduced new member in custom type and explicitly queried for it.
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.Visiting.Patient
引入后 ScheduleView.Patient
我发现它也装载。
不能得到的EF逻辑,在这种情况下。而且不知道如何强制加载 ScheduleView.Visiting.Patient
而无需加载无用 ScheduleView.Patient
:(
Now I have Patient
loading properly in my custom type. Amusing, but when I investigate ScheduleView.Visiting.Patient
after introducing ScheduleView.Patient
I found it also loaded.
Cant get the EF logic in this case. And dunno how to force loading ScheduleView.Visiting.Patient
without having to load useless ScheduleView.Patient
:(
这篇关于实体框架:如何做正确的"包括"在自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!