Entity Framework - Inheritance with .Include?

前端 未结 4 1488
挽巷
挽巷 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:32

    I've come across this problem and experimented a little. Here is what i found worked using LINQ 2 Entity.

    Say we have in your example Person <-- Developer ---- Qualifications.

    If you would like to select a Developer with Qualifications included, you would do this.

    var dev = (from d in context.Persons.OfType
                                .Include("Qualifications")
              where d.ID == id
              select d).FirstOfDefault();
    

    Now lets say we have another association between Person and Address, we can also include Address into the select also using LINQ 2 Entity.

    var dev = (from d in context.Persons
                                .Include("Address")
                                .OfType()
                                .Include("Qualifications")
              where d.ID == id
              select d).FirstOfDefault();
    

    Notice how I have included the things I needed before I converted the Type and also included again after the conversion. Both includes should now work together without any problems. I have tested these methods and they both work. I hope they work for you given you have your inheritance setup correctly.

    GL.

提交回复
热议问题