3

I am using Entity Framework 4, having the circular reference among some of entities, but I am getting stackoverflow exception, this is the scenario when lazy loading is enabled...

Then I have made lazy loading enabled as false and include all the related entities using Include() method but still am getting the same error... Help needed....

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Manish Pandey
  • 175
  • 2
  • 13

2 Answers2

7

I came across this before - I solved it by adding the [IgnoreDataMember] attribute to the property causing the circular reference. This was usually the navigation property that referenced the parent entity.

Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
  • Thanks but we have solved the problem by using DTOs(instead of exposing entites directly to the client), basically its architecture problem and we should avoid it as far as possible... – Manish Pandey May 21 '12 at 17:18
  • @davel i realize this answer is quite old, but when you run into situations like this, are you putting the attribute on the EF code first generated class? What happens when you regenerate your model from db (if going that route) and the class is automatically regenerated without that attribute? – Kritner Aug 26 '14 at 19:39
  • Hmm we generate model from db too... I presume it would have to be a manual step after recreating model from db. I can't even remember where I even used this... – Dave Lawrence Aug 28 '14 at 13:28
  • @daveL Thanks for mentioning IgnoreDataMember Attribute. It saved my day. – Vishal Jun 05 '16 at 06:10
1

If you can't remove your cycles then something like WCF cyclic references should fix it

Olav Nybø
  • 11,454
  • 8
  • 42
  • 34