Loading Nested Entities / Collections with Entity Framework

后端 未结 2 1042
离开以前
离开以前 2020-12-14 19:05

I am trying to Eagerly load all the related entities or collection of Entity in one call. My Entities Looks like:

Class Person
{
    public virtual long Id {         


        
相关标签:
2条回答
  • 2020-12-14 19:26

    Since this is the first page in my search in google, I just wanted to post this.

    Slauma's answer is fine. But it's recommended to use Load() instead of ToList() if you don't plan to actually use the list. So it would be:

        Context.Employees
            .Include(e => e.Person)
            .Include(e => e.Titles.Select(t => t.Title))
            .Load();
    
    0 讨论(0)
  • 2020-12-14 19:29

    You can try this:

    Context.Employees
        .Include(e => e.Person)
        .Include(e => e.Titles.Select(t => t.Title))
        .ToList();
    

    Select can be applied to a collection and loads navigation properties of the next level in the object graph.

    0 讨论(0)
提交回复
热议问题