0

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.

4
  • 4
    If you don't want C then why include it? On the other hand, if C is literally the same object, then it's not duplicating data. Commented May 4, 2016 at 15:30
  • What do you get from simply using Context.A.Where(...) ? What is that missing that you need to include? Commented May 4, 2016 at 15:33
  • i want c, i just dont want it twice. its coming duplicated on both objects, A and B. Commented May 4, 2016 at 17:14
  • And yes Erick, it's duplicating data. My response come with the full object C in both, A and B. Anyways, looks like nobody here can help me. These days people prefer to be ironic the help others here. Commented May 4, 2016 at 18:12

1 Answer 1

1

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) 
Sign up to request clarification or add additional context in comments.

3 Comments

In my opinion, with the query i made the correct is to load only a.b and a.c. If i wanted b.c i should write .Include(x => x.b.c). I'm not loading b.c but its coming filled, duplicating a lot of data.
It doesn't duplicate data. a.b.c and a.c reference to the same memory location.
well, my json response shoul contain only one object of type C. it's coming a lot bigger because of this. to me it's duplicating data :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.