EF6 include with join not including.

后端 未结 1 1752
滥情空心
滥情空心 2021-01-27 14:39

I\'m well aware this is an issue with a clear solution in more simple situations, but i have implemented something that i think doing it otherwise or changing it completely due

相关标签:
1条回答
  • 2021-01-27 15:19

    Include never works if you project to an (anonymous or named) type. Think of it, in which class should EF include the child collection?

    The include will work if you do

    var query = from mission in context.Missions.Include("AssignedAgents")
        join client in context.Clients on mission.ClientId equals client.Id
        select mission
    

    Then EF will have Mission objects for which it can load AssignedAgents collections.

    If you don't want to get the full objects you can get the same effect by incorporating AssignedAgents as a property in the anonymous type:

    var query = from mission in context.Missions
        join client in context.Clients on mission.ClientId equals client.Id
        select new
            {
                mission,
                client.Name,
                client.Office,
                client.Id,
                Agents = mission.AssignedAgents
            };
    
    0 讨论(0)
提交回复
热议问题