none
EF eager loading: How include works RRS feed

  • Question

  • 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:27 PM

Answers

  • Hi,

    >>include create join operation to fetch data from multiple table ?

    Unlike lazy loading, EF use include method to specifies the related objects to include in the query results.

    That mean EF would create one join query statement to load related objects one time into memory.

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

    If there is not relations between the tables, then it has no way of using .Include() which would create joins in the SQL call.

    The query will failed,  if you catch the exception, you will get "A specified Include path is not valid. The EntityType 'xxxx.Companies' does not declare a navigation property with the name 'XXX'".

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

    The basic shape of the model is detected by using conventions, you can further configure your model by using data annotations or the fluent API.

    To understand this ,please see following articles:

    https://msdn.microsoft.com/en-us/library/jj679962(v=vs.113).aspx

    https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx

    https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113).aspx

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Tuesday, December 12, 2017 12:52 PM
    Monday, December 11, 2017 11:33 AM