Strange behavior of INCLUDE in Entity Framework

后端 未结 2 1029
后悔当初
后悔当初 2021-01-24 14:02

This works:

using (var dbContext = new SmartDataContext())
{
    dbContext.Configuration.ProxyCreationEnabled = false;

    var query = dbContext.EntityMasters.O         


        
相关标签:
2条回答
  • 2021-01-24 14:11

    Entity framework's 'Include' function only works when it is connected to the entire linq query that was looking up the entity. This is because the linq query is actually a form of Expression that can be inspected as a whole before it is executed.

    In the second example there the Person object is already detached from the database so EF has no information on which table Person came from and how it should join Person with the address table to get the results you want.

    If you turn on dynamic proxy generation EF is able to keep track of the relation between the entity and the database. However, I'm not sure if this will make the include statement work.

    0 讨论(0)
  • 2021-01-24 14:13

    You need to assign the result of Include back to query

    query = query.Include(p => p.Addresses);
    
    0 讨论(0)
提交回复
热议问题