Entity Framework - Include Multiple Levels of Properties

前端 未结 8 853
囚心锁ツ
囚心锁ツ 2020-11-22 08:43

The Include() method works quite well for Lists on objects. But what if I need to go two levels deep? For example, the method below will return ApplicationServers with the i

8条回答
  •  忘掉有多难
    2020-11-22 09:16

    I made a little helper for Entity Framework 6 (.Net Core style), to include sub-entities in a nice way.

    It is on NuGet now : Install-Package ThenInclude.EF6

    using System.Data.Entity;
    
    var thenInclude = context.One.Include(x => x.Twoes)
        .ThenInclude(x=> x.Threes)
        .ThenInclude(x=> x.Fours)
        .ThenInclude(x=> x.Fives)
        .ThenInclude(x => x.Sixes)
        .Include(x=> x.Other)
        .ToList();
    

    The package is available on GitHub.

提交回复
热议问题