How to remove circular reference in Entity Framework?

前端 未结 6 1933
借酒劲吻你
借酒劲吻你 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:02

    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.

    0 讨论(0)
  • 2021-01-12 08:13

    The changing getter to internal for a child navigation worked for me with entitfy framework v5/v6 under Web API v2

    0 讨论(0)
  • 2021-01-12 08:14

    Watch this page I hope I could do to survive
    http://msdn.microsoft.com/en-us/data/jj574232.aspx

    0 讨论(0)
  • 2021-01-12 08:16

    On serverlevel:

    [DataContract(IsReference = true)] 
    

    MSDN

    0 讨论(0)
  • 2021-01-12 08:21

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

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题