How to remove circular reference in Entity Framework?

前端 未结 6 1980
借酒劲吻你
借酒劲吻你 2021-01-12 07:36

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 betwe

6条回答
  •  日久生厌
    2021-01-12 08:29

    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);
    

提交回复
热议问题