Entity Framework - Include multiple level properties
Add another Include
call:
entity.TableLevel1.Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.TableLevel3))
.Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.AnotherTableLevel3));
If you want to load related entities that are at the same level you should call Include
extension method for each of them.
You can make multiple Include()
calls:
entity.TableLevel1.Include(t1 => t1.TableLevel2);
entity.TableLevel1.Include(t1 => t1.TableLevel2.Select(t2 => t2.TableLevel3));
entity.TableLevel1.Include(t1 => t1.TableLevel2.Select(t2 => t2.AnotherTableLevel3));
or
entity.TableLevel1.Include("TableLevel2");
entity.TableLevel1.Include("TableLevel2.TableLevel3");
entity.TableLevel1.Include("TableLevel2.AnotherTableLevel3");
But you can mark your navigation properties as virtual
and will be lazy loading, so you dont need to make the Include()
calls:
class TableLevel1
{
public virtual TableLevel2 TableLevel2 { get; set; }
}
class TableLevel2
{
public virtual TableLevel3 TableLevel3 { get; set; }
public virtual TableLevel3 AnotherTableLevel3 { get; set; }
}