In Entity Framework, how do you get rid of a circular reference?

.net entity-framework orm

Question

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!

1
8
8/13/2015 8:09:24 AM

Accepted Answer

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);
13
1/21/2010 1:28:17 PM

Popular Answer

I have solved this problem in EF 3.5 By changing the Child's navigation property Getter from public to Internal.



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