C# 错误:序列化实体框架类

C# 错误:序列化实体框架类,c#,entity-framework-4,C#,Entity Framework 4,如何在不删除虚拟关键字的情况下解决此问题?请共享 // @这是我的模型 Error :' A circular reference was detected while serializing an object of type 'CGWeb.Models.Repositories.Models.Event'. [表格(“表格事件”)] 公开部分班级活动 { [关键] 公共int ID{get;set;} //public int?LocationId{get;set;} //public i

如何在不删除虚拟关键字的情况下解决此问题?请共享

//

@这是我的模型

Error :' A circular reference was detected while serializing an object of type 'CGWeb.Models.Repositories.Models.Event'.
[表格(“表格事件”)]
公开部分班级活动
{
[关键]
公共int ID{get;set;}
//public int?LocationId{get;set;}
//public int?ImageId{get;set;}
公共字符串EventName{get;set;}
[未映射]
公共字符串事件地址{get;set;}
公共字符串EventUrl{get;set;}
公共字符串EventDesc{get;set;}
公共可为空的起始日期{get;set;}
公共可为空的结束日期{get;set;}
公共可为空的EventCategoryID{get;set;}
public int CityID{get;set;}
公共整数{get;set;}
[ForeignKey(“EventCategoryID”)]
公共虚拟事件类别{get;set;}
//[外键(“图像ID”)]
[未映射]
公共虚拟ImageViewModel映像{get;set;}
//[外键(“位置ID”)]
//公共虚拟位置{get;set;}
[外键(“CityID”)]
公共虚拟城市城市{get;set;}
[未映射]
公共bool isponsorship{get;set;}
[未映射]
公共组织者{get;set;}
//[未映射]
[外键(“事件ID”)]
公共虚拟IList{get;set;}
}

这与
virtual
关键字无关;它与对象图有关。我们看不到您的图形,但这里的经典场景是父/子双向关系,即父级有一个
.Children
,子级有一个
.parent

树序列化程序(如xml、json等)通常会遍历未显式标记为忽略的任何成员。因此,你会得到一个无限的循环,因为它永远围绕着那个圆。选项:

  • 在此边界处使用非循环DTO(这就是我要做的)
  • 将有问题的反向引用标记为排除(此机制因序列化程序而异)

您确定日期比较吗?6月12日不会在7月1日之前……先生,你能告诉我这两种选择吗。“我该怎么解决这个问题呢?”贾斯汀戴伊没有看到你的对象模型,不是真的。对于JavaScriptSerializer,您通常会使用
[ScriptIgnore]
来告诉它忽略它,但是如果生成了您的模型,那么这可能是不可能的-在这种情况下,我不会在最后一步中使用EF模型,但是,将数据投影到一个简单的平面类模型中,该模型准确地表示您希望json的外观。如果我将我的模型放入其中,请查看。。!
Error :' A circular reference was detected while serializing an object of type 'CGWeb.Models.Repositories.Models.Event'.
 [Table("Table_Events")]
public partial class Event
{
    [Key]
    public int ID { get; set; }

    //public int? LocationId { get; set; }
    //public int? ImageId { get; set; }
    public string EventName { get; set; }

    [NotMapped]
    public string EventAddress { get; set; }

    public string EventUrl { get; set; }
    public string EventDesc { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> EndDate { get; set; }

    public Nullable<int> EventCategoryID { get; set; }
    public int CityID { get; set; }
    public int Viewed { get; set; }

    [ForeignKey("EventCategoryID")]
    public virtual EventCategory EventCategory { get; set; }
    //[ForeignKey("ImageId")]
    [NotMapped]
    public virtual ImageViewModel Image { get; set; }

    //[ForeignKey("LocationId")]
    //public virtual Location Location { get; set; }


    [ForeignKey("CityID")]
    public virtual City City { get; set; }

    [NotMapped]
    public bool ISSponsorship { get; set; }

    [NotMapped]
    public Organizer Organizer { get; set; }

    //[NotMapped]
    [ForeignKey("EventId")]
    public virtual IList<Attending> Attending { get; set; }
}