我已经阅读了很多此问题的解决方案,但都没有为我工作。检测到自检回路
我有事件和用户对象之间的循环关系:
public class Event : EntityData
{
[Required]
[ForeignKey("Creator")]
public string CreatorId { get; set; }
public User Creator { get; set; }
[InverseProperty("ParticipantIn")]
public virtual ICollection<User> Participants { get; set; }
[InverseProperty("ManagerIn")]
public virtual ICollection<User> Managers { get; set; }
}
正如你可以看到我有这个类用户三个参考:事件的创造者,管理人员的名单,和与会者名单。
用户类包含对事件的引用,以及:
public class User: EntityData
{
[InverseProperty("Participants")]
public virtual ICollection<Event> ParticipantIn { get; set; }
[InverseProperty("Managers")]
public virtual ICollection<Event> ManagerIn { get; set; }
}
现在,问题是,当我试图序列的事件,就像在我createEvent功能,它告诉我,有一个自我引用循环,这是因为当创建事件时,我将它添加到创作者的'ManagerIn'集合中。
这行导致事件 - >创建器> ManagerIn->事件 - >创作者 - > ..... LOOP
我想什么,我也有一个版本的代码的公共虚拟用户的创造者,为了使其加载懒洋洋地..
现在,我的解决方案是非常不优雅,事件返回到客户端之前,我执行:
event.Creator = null;
和
event.Managers = null;
为了避免自引用循环。
解决这类问题的正确方法是什么?
在此先感谢, Liran!
你用什么来序列化它? – Evk
如果您使用的是XML http://stackoverflow.com/questions/5004397/is-it-possible-to-perform-serialization-with-circular-references可能是相关的 – Chris
http://stackoverflow.com/questions/ 26434738 /你怎么真的序列化循环引用对象与牛顿软件json可能是有趣的json – Chris