简体   繁体   中英

Entity Framework - Include with multiple levels

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM