【问题标题】:Entity Framework WebApi Circular dependency serialization errorEntity Framework WebApi循环依赖序列化错误
【发布时间】: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 实体时,出现循环依赖错误。我尝试过的事情:

  1. 在 DbContext 中禁用延迟加载。
  2. 添加 json.SerializerSettings.PreserveReferencesHandling=Newtonsoft.Json.PreserveReferencesHandling.All;到 GLOBAL.asax

  3. 在每个子实体的 TripId 中添加一个装饰器 [IgnoreDataMember]。

  4. 将此实体映射到不包含 ICollection 成员的 ViewModel。 - 这行得通,但在某些时候我会想要将这些列表提供给客户。

我真的不知道发生了什么。我错过了什么?我真的找不到任何循环依赖。

【问题讨论】:

    标签: asp.net json entity-framework asp.net-web-api circular-dependency


    【解决方案1】:

    您是否尝试将[JsonIgnore] 属性添加到子实体的TripId

    http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm

    或设置

    json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    【讨论】:

    • 将其添加到子实体中的 TripId 中?
    • 在这两种情况下都是一样的:/
    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多