locked
EF eager loading: How include works RRS feed

  • Question

  • User1183902823 posted

    just see the code

    var company = context.Companies
                         .Include(co => co.Employees.Select(emp => emp.Employee_Car))
                         .Include(co => co.Employees.Select(emp => emp.Employee_Country))
                         .FirstOrDefault(co => co.companyID == companyID);

    1) include create join operation to fetch data from multiple table ?

    2) what will happen if there is no relation between two table then how include will work? throw any error ?

    3) how include understand which columns will be used to join between two table ?

    i read this link https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx but still not very clear that how include works

    please guide me.

    Sunday, December 10, 2017 4:05 PM

Answers

  • User-707554951 posted

    Hi tridip1974, 

    how include determine which field will be used in join

    Include() method will result in Left out join query, one database trip

    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public byte[]  Photo { get; set; }
        public decimal Height { get; set; }
        public float Weight { get; set; }
            
        public Standard Standard { get; set; }
    }
    
    public class Standard
    { 
        }
        public int StandardId { get; set; }
        public string StandardName { get; set; }
        
        public ICollection<Student> Students { get; set; }
       
        }
            

    As you can see in above example. it has Students and Standards class. StudentId and StandardId as PK (primary key), it will generate an Standard_StandardId column as FK (foreign key).

    So, EF will know use which column field used in join by check navigation because EF infer the One-to-Many relationship using navigation property by default convention

          .Include(co => co.Standard .Select(emp => emp.StandardName))

    If we have above include statement, ef will go to Standard class and get it's primary key(StandardId) then use StandardId

     to do left join

    http://www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx

    http://www.entityframeworktutorial.net/code-first/code-first-conventions.aspx

    Best regards 

    Cathy 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 12, 2017 10:03 AM

All replies

  • User991499041 posted

    Hi Tridip1074,

    1) include create join operation to fetch data from multiple table ?

    2) what will happen if there is no relation between two table then how include will work? throw any error ?

    3) how include understand which columns will be used to join between two table ?

    Eager loading related entity through reference navigation(virtual) property.

    In EF, eager loading is translated to a single LEFT OUTER JOIN query.

    If we remove virtual properties, it would throw error.

    Use Eager Loading when the relations are not too much. 

    Use Eager Loading when you are sure that you will be using related entities with the main entity everywhere.

    ThenInclude method was added in EF Core, which is the last version of EF.

    Regards,

    zxj

    Monday, December 11, 2017 2:31 AM
  • User1183902823 posted

    i heard eager loading does the left join ma i right ?

    i like to know how eager loading understand which columns will be used for joining between two tables ?

    Monday, December 11, 2017 9:49 AM
  • User753101303 posted

    Yes it does a join (it is more efficient in EF Core if I remember) but anyway it's a tradeoff and you should do that if it's worth.. It will use what you declared for your entities. I never tried without a relation. I would have to try if it fails or if it just load all "included" entities.

    Do you have an actual error ? If yes, it's likely best to discuss directly the error.

    Monday, December 11, 2017 9:57 AM
  • User1183902823 posted

    i am learning EF so this question come to my mind which i ask here.

    why you said eager loading has trade off ? come with explanation and sample....because i need to have good knowledge about it reason i am learning it now.

    some guys said lazy load has trade of when we load child data for each parent data. suppose there is huge child data for each parent then people suggesting not use lazy load rather use eager loading to save database round trip.....is it right?

    you did not answer the most important question. how include determine which field will be used in join?

    say i am fetching department and employee data using include keyword.

    department table has ID column and dept is FK in employee table. so tell me how include will understand that it need to join ID and dept?

    please answer this point in detail. thanks

    Monday, December 11, 2017 12:57 PM
  • User-707554951 posted

    Hi tridip1974, 

    how include determine which field will be used in join

    Include() method will result in Left out join query, one database trip

    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public byte[]  Photo { get; set; }
        public decimal Height { get; set; }
        public float Weight { get; set; }
            
        public Standard Standard { get; set; }
    }
    
    public class Standard
    { 
        }
        public int StandardId { get; set; }
        public string StandardName { get; set; }
        
        public ICollection<Student> Students { get; set; }
       
        }
            

    As you can see in above example. it has Students and Standards class. StudentId and StandardId as PK (primary key), it will generate an Standard_StandardId column as FK (foreign key).

    So, EF will know use which column field used in join by check navigation because EF infer the One-to-Many relationship using navigation property by default convention

          .Include(co => co.Standard .Select(emp => emp.StandardName))

    If we have above include statement, ef will go to Standard class and get it's primary key(StandardId) then use StandardId

     to do left join

    http://www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx

    http://www.entityframeworktutorial.net/code-first/code-first-conventions.aspx

    Best regards 

    Cathy 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 12, 2017 10:03 AM
  • User1183902823 posted

    @Cathy i guess this is missing public int StandardId { get; set; } in student class.

    this one is right .Include(co => co.Standard .Select(emp => emp.StandardName))

    please give me a full EF linq query code for student and standard with include keyword.

    anyway thanks for your answer.

    Tuesday, December 12, 2017 11:06 AM
  • User753101303 posted

    You'll load all related data regardless of wether or not they are actually used. It is done using a single round trip with joins which can create a huge resultset if you are using too many includes.

    Another strategy would be to do multiple queries but then you may have to relate data one to each other etc... (EF Core should do something similar if I remember).

    Or if you use only a small portion doing additonal queries could be better.

    In short see use include if you really need almost all (or a least a verty large fraction) of what you are loading.

    Tuesday, December 12, 2017 12:16 PM