Entity Framework - Inheritance with .Include?

前端 未结 4 1515
挽巷
挽巷 2021-01-02 09:05

I think that there\'s a similar post on here about this but not exactly the same...

I have two entities in my EF model - let\'s call them Person and Developer, with

4条回答
  •  遥遥无期
    2021-01-02 09:09

    How about this:

    var results = from developer in ctx.Employee.OfType()
                  select new {developer, Qualifications = developer.Qualifications};
    

    To things are interesting here:

    1. we are excluding employees that aren't Developers
    2. we are then projecting their Qualifications, and as a side effect of that projection, something called fixup with fill each developer.Qualifications too. I.e. this is another way of achieve the same effect as Include().

    if you subsequently do this:

    var developers = from anon in developers.AsEnumerable()
                     select anon.Developer;
    

    You will get just developers, and they will have their Qualifications loaded too.

    See Tip 1 for more info on why this works

    Hope this helps

    Alex

提交回复
热议问题