The circular reference between my Customer and Order entities caused a exception during serialization. Is there any way to force EF to generate one-direction reference between these two entities? Thanks in advance!
6 Answers
When I need to serialize, I generally project onto other types. This eliminates circular references, plus other data I don't want serialize. For example:
var q = (from c in Repository.Customers()
where c.Id == id
select new
{
Name = c.Name,
Orders = from o in C.Orders
select new
{
Date = o.Date
}
}).First();
return Json(q);

- 125,891
- 12
- 252
- 273
When you create an association in model designer (right click add->association) you'll get a popup windows which looks like this:
Notice the navigation property check boxes, you can deselect them if you don't want them to be generated. To solve your circular reference problem, make sure only one or none are checked, not both.

- 21,988
- 13
- 81
- 109

- 10,227
- 10
- 51
- 92
-
Sorry, I can't catch up what you said. Can you put it more specify? Thanks! – Roy Jan 21 '10 at 07:34
-
Thanks Charlie, but I cann't see the picture. And my designer don't have checkbox on it. :( I'm using Visual Studio 2008. – Roy Jan 21 '10 at 10:08
-
Ok, I'm using Entity Framework 4.0 on VS2010 beta 2...maybe this isn't something you can do in the older version of EF. – Charlie Jan 21 '10 at 10:42
-
2In 3.5 SP1 you can't remove navigation properties using the designer, you have to resort to removing them from the ConceptualModel section of the XML inside the EDMX file. However I think Craig's answer is much better. You don't necessarily want to change you model to solve a serialization problem. There is only so much you can do by changing the model. – Alex James Jan 21 '10 at 17:01
-
@Charlie, James: Thanks a lot. I'm going to add Dto to my project. – Roy Jan 22 '10 at 06:10
-
@Alex James. Yep I'll go with that as well, Craigs solution is preferable – Charlie Jan 22 '10 at 09:39
I have solved this problem in EF 3.5 By changing the Child's navigation property Getter from public to Internal.

- 430
- 1
- 6
- 22
-
Thanks, that worked for me. Not entirely sure if it's the right way to do it though! – Tom Hall Aug 12 '13 at 12:57
Watch this page
I hope I could do to survive
http://msdn.microsoft.com/en-us/data/jj574232.aspx

- 108
- 10
-
3Note that [link-only answers are discouraged](http://meta.stackoverflow.com/tags/link-only-answers/info), SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Oct 13 '13 at 09:36
The changing getter to internal for a child navigation worked for me with entitfy framework v5/v6 under Web API v2

- 21
- 1