Reputation: 3883
I am trying to get a List<EntityType>
, each item has about 15 navigation properties that I need to load, some of navigations properties have a navingation properties that I need to load too, this is a sample code
class AA
{
public EntityReference<B> Bobj
{
get;
set;
}
public EntityCollection<C> CCollection
{
get;
set;
}
}
class B
{
public EntityCollection<X> XCollection
{
get;
set;
}
}
class C
{
public EntityReference<Y> YObj
{
get;
set;
}
}
List<AA> AList = Dbcontext.AAs.Where(a => a.ID==4).ToList();
I want each item in List<AA>
has been loaded with its BObj
with its XCollection
and and all CCollection
items with its YObj
of each.
I tried to load them using Include
and Load
but I think there is an elegant way to do that
also I tried to set LazyLoadingEnabled
to false
but that does't load the full graph I don't know why?
so please I need the best way to load the full graph of my EntityType
I am using Database first approach EF 4.1
and C# 4.0
Upvotes: 2
Views: 527
Reputation: 156459
Here's a neat trick I've picked up:
List<AA> AList = Dbcontext.AAs.Where(a => a.ID==4)
.Select(a => new {a, a.Bobj, a.CCollection})
.ToList().Select(o => o.a).ToList();
Dbcontext.Bs.Where(b => b.A.ID==4)
.Select(b => new {b, b.XCollection})
.ToList();
Dbcontext.Cs.Where(c => c.A.ID==4)
.Select(c => new {c, c.YObj})
.ToList();
Entity Framework will figure out how these various objects are connected. You'll be making a manageable number of round-trips (3 in this case), and the SQL query results produced will have relatively little repeated data.
Upvotes: 5
Reputation: 1706
Turning lazy-loading off would prevent them from loading. Turn lazy-loading on and mark all navigation properties as virtual, this will allow them to be overridden so that dynamic proxies can be created. This will allow lazy-loading to work properly. Explicit loading is also an option (and considered by many to be better coding practice).
Edit: Oh I thought it was code first as well, excuse me. Then, if it helps, since Include returns a query object, you can actually loop through a list of them and add them to themselves to build your query dynamically.
Upvotes: 0