1

我想,我已經閱讀了關於這個錯誤的所有信息,並且我嘗試了一切。這裏是我的模型:實體框架WebApi循環依賴序列化錯誤

主營:

public class Trip 
{ 
    public int TripId { get; set; } 
    public string Name { get; set; } 
    public string ShortDescription { get; set; } 
    public string Country { get; set; } 
    public float BasicPrice { get; set; } 
    public virtual ICollection<ApartmentType> ApartmentType { get; set; } 
    public virtual ICollection<TransportMethod> TransportMethod { get; set; } 
    public virtual ICollection<FeedingType> FeedingType { get; set; } 
} 

ApartmentType:

public class TransportMethod 
{ 
    public int TransportMethodId { get; set; } 
    public int TripId { get; set; } 
    public string Name { get; set; } 
    public float Price { get; set; } 
} 

FeedingType:

public class FeedingType 
{ 
    public int FeedingTypeId { get; set; } 
    public int TripId { get; set; } 
    public string Description { get; set; } 
    public float Price { get; set; } 
} 

TransportType:

public class TransportMethod 
{ 
    public int TransportMethodId { get; set; } 
    public int TripId { get; set; } 
    public string Name { get; set; } 
    public float Price { get; set; } 
} 

當serializng Trip實體時,我得到一個循環依賴錯誤。我試過的東西:

  1. 在DbContext中禁用延遲加載。
  2. 加入 json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;到GLobal.asax

  3. 在每個子實體中添加一個裝飾器[IgnoreDataMember]到TripId。

  4. 將此實體映射到不包含ICollection成員的ViewModel。 - 這工作正常,但在某些時候我會想要將這些列表提供給客戶端。

我真的不知道發生了什麼事。我錯過了什麼?我真的不能發現任何循環依賴。

回答