【发布时间】:2012-12-06 21:24:11
【问题描述】:
我想,我已经阅读了有关此错误的所有内容并尝试了所有内容。这是我的模型:
主要:
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; }
}
公寓类型:
public class TransportMethod
{
public int TransportMethodId { get; set; }
public int TripId { get; set; }
public string Name { get; set; }
public float Price { get; set; }
}
喂养类型:
public class FeedingType
{
public int FeedingTypeId { get; set; }
public int TripId { get; set; }
public string Description { get; set; }
public float Price { get; set; }
}
传输类型:
public class TransportMethod
{
public int TransportMethodId { get; set; }
public int TripId { get; set; }
public string Name { get; set; }
public float Price { get; set; }
}
序列化 Trip 实体时,出现循环依赖错误。我尝试过的事情:
- 在 DbContext 中禁用延迟加载。
添加 json.SerializerSettings.PreserveReferencesHandling=Newtonsoft.Json.PreserveReferencesHandling.All;到 GLOBAL.asax
在每个子实体的 TripId 中添加一个装饰器 [IgnoreDataMember]。
- 将此实体映射到不包含 ICollection 成员的 ViewModel。 - 这行得通,但在某些时候我会想要将这些列表提供给客户。
我真的不知道发生了什么。我错过了什么?我真的找不到任何循环依赖。
【问题讨论】:
标签: asp.net json entity-framework asp.net-web-api circular-dependency