开发者

EF Table Per Type with Navigation property on Sub Type Table

开发者 https://www.devze.com 2023-02-01 12:27 出处:网络
I\'m hoping someone out in the SO community will be able to help me out here. Simplified Background: I\'m using Entity Framework V1 to build my class structure that is outlined below, I\'m using Tabl

I'm hoping someone out in the SO community will be able to help me out here.

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:

Employee 
CaseA : Case
CaseB : Case
CaseC : Case

CaseB has a Navigational Property to Employee

I have a Repository that r开发者_如何学JAVAeturns an ObjectQuery. If the type of Case is actually CaseB, I need to include the Employee object within the graph. I can't .Include("Employee") because it's not a navigational property of Case, and Employee doesn't have a .Load() method on it.

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)

    //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.

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.

The above should work - just make sure you do the eager load after you do the OfType.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号