我想,我已經閱讀了關於這個錯誤的所有信息,並且我嘗試了一切。這裏是我的模型:實體框架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實體時,我得到一個循環依賴錯誤。我試過的東西:
- 在DbContext中禁用延遲加載。
加入 json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;到GLobal.asax
在每個子實體中添加一個裝飾器[IgnoreDataMember]到TripId。
- 將此實體映射到不包含ICollection成員的ViewModel。 - 這工作正常,但在某些時候我會想要將這些列表提供給客戶端。
我真的不知道發生了什麼事。我錯過了什麼?我真的不能發現任何循環依賴。
將它添加到TRIPID在兒童實體? –
是的,看我更新的答案 – kzhen
在這兩種情況下都是一樣的:/ –