Entity Framework - Include with multiple levels

asp.net c# entity-framework-6

Question

If I have 3 classes A, B and C:

class A {
    public B b { get; set; }
    public C c { get; set; }
}

class B {
    public C c { get; set; }
}

class C {
    ...
}

When I make a query like:

Context.A.Where(...).Include(x => x.b).Include(x => x.c);

Entity loads the C object in both A and B, duplicating data. Is there a way to prevent it? I don't use lazy loading.

1
0
5/4/2016 3:29:07 PM

Popular Answer

You instruct Entity Framework to load the b and c entities with the A set. Since the b items contain references to items of type C that might be actually the same loaded items with the a objects, then Entity Framework will use them to construct the objects graph. That doesn't mean Entity Framework generates an extra SQL join to load the c items for the B type. To do this you would write

Context.A.Where(...).Include(x=>x.b).Include(x=>x.c).Include(x=>x.b.c)
1
5/4/2016 3:56:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow