代码之家  ›  专栏  ›  技术社区  ›  Konstantin Chernov

实体框架:如何对自定义类型进行正确的“包含”

  •  4
  • Konstantin Chernov  · 技术社区  · 14 年前

    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})
    

    Patient 财产 Visit 键入。但是当我写作的时候

    query = (query as ObjectQuery<ScheduleView>).Include("Visit.Patient");
    

    无法转换类型 '系统.Linq.IQueryable 1' to type 'System.Data.Objects.ObjectQuery 1英寸。 实体数据模型原语类型。

    所以,问题是-如何强制查询在我的自定义类型中包含某些内容?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Konstantin Chernov    11 年前

    最后,开发了一些难看的解决方法-在自定义类型中引入新成员并显式查询它。

    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**})
    

    现在我有了 Patient 在我的自定义类型中正确加载。很有趣,但是当我调查的时候 ScheduleView.Visiting.Patient ScheduleView.Patient 我发现它也装了子弹。 在这种情况下不能得到EF逻辑。不知道怎么强迫 不用装无用的东西 计划视图。患者 :(

        2
  •  0
  •   RPM1984    14 年前

    你可以 当然是这种类型,但不会影响查询。

    这样行吗?(头顶上,未经测试)

    var scheduleViews = (from s in ctx.Schedules
                         join v in ctx.Visits.Include("Patient") 
                         on v.ScheduleId equals s.ScheduleId
                         select new ScheduleView 
                         { 
                            Schedule = s, 
                            Visit = v 
                         }).ToList();
    

    你的主要问题是 ObjectQuery<ScheduleView> 无法转换为存储表达式。所以你在 Visits

    高温高压