简体   繁体   English

每种类型的EF表,子类型表具有导航属性

[英]EF Table Per Type with Navigation property on Sub Type Table

I'm hoping someone out in the SO community will be able to help me out here. 我希望SO社区中的某人能够在这里为我提供帮助。

Simplified Background: I'm using Entity Framework V1 to build my class structure that is outlined below, I'm using Table Per Type to persist my inherited objects: 简化的背景:我正在使用Entity Framework V1构建下面概述的类结构,我正在使用“每种类型的表”保留继承的对象:

Employee 
CaseA : Case
CaseB : Case
CaseC : Case

CaseB has a Navigational Property to Employee CaseB对员工具有导航属性

I have a Repository that returns an ObjectQuery. 我有一个返回ObjectQuery的存储库。 If the type of Case is actually CaseB, I need to include the Employee object within the graph. 如果Case类型实际上是CaseB,则需要在图中包含Employee对象。 I can't .Include("Employee") because it's not a navigational property of Case, and Employee doesn't have a .Load() method on it. 我不能使用.Include(“ Employee”),因为它不是Case的导航属性,并且Employee上没有.Load()方法。

Ideally I want to be able to do this in one query, however as a fall back I'm happy that I make a call, check the Object and perform another call, something like this: (although as I stated earlier, load doesn't exist on the employee navigational property) 理想情况下,我希望能够在一个查询中执行此操作,但是作为一个后退,我很高兴能够打电话,检查Object并执行另一个调用,如下所示:(尽管如前所述,加载不会t存在于员工导航属性上)

    //Get the case from the 
    Case myCase = new Repo<Case, Entities>.FirstOrDefault();

    if(myCase is CaseB)
       ((CaseB)myCase).Employees.load();

Am I missing something really simple here? 我在这里错过了一些非常简单的事情吗?

Try this: 尝试这个:

var employee = ctx.Cases
                  .OfType<CaseB>()
                  .Include("Employees")
                  .Select(x => x.Employees)
                  .FirstOrDefault();

OfType<T>() is one of the most important methods in EF when it comes to inheritance - you should familiarize yourself with it. 在继承方面, OfType<T>()是EF中最重要的方法之一-您应该熟悉它。

Essentially is filters the items in the query to be of a particular type - very similar to the conditional check your doing in your answer. 本质上是将查询中的项目过滤为特定类型-与您在回答中所做的条件检查非常相似。

It's an IQueryable method (LINQ-Objects), but in LINQ-Entities ( ObjectQuery<T> ), it get's implemented as an INNER JOIN. 这是一个IQueryable方法(LINQ-Objects),但是在LINQ-Entities( ObjectQuery<T> )中,它被实现为INNER JOIN。

The above should work - just make sure you do the eager load after you do the OfType . 上面的应该起作用-只要确保执行OfType 之后就执行了OfType加载。

HTH. HTH。

As always, after posting the question I found this and this which has pointed me towards using projection (solution below), but I was hoping to avoid that, so I'm going to leave the question open to see if there is a more elegant solution. 和往常一样,在发布问题后,我发现了这个问题, 使我倾向于使用投影(下面的解决方案),但是我希望避免这种情况,所以我将让这个问题保持开放状态,看看是否有更优雅的方法解。

var Employee = caseObjectQuery.Select(x => new{
                Employee = x is CaseB ? (x as CaseB).Employee : null
            }
            ).FirstOrDefault();

Just by selecting the Object into memory, EF magically maps the related entities. 只需将对象选择到内存中,EF即可神奇地映射相关实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM