Error:serializing Entity Framework class

二次信任 提交于 2019-12-08 03:58:26

问题


    public IList<Event> SearchEvents(DateTime fromDate, DateTime toDate, int categoryId, string eventName )
    {

        var  query = context.Events.Include("City").Where(e => e.EventCategoryID == (categoryId <= 0 ? e.EventCategoryID : categoryId)
            && (e.StartDate.Value.Month >= fromDate.Month)
            && (e.EndDate.Value.Month <= toDate.Month)
            && ( e.StartDate.Value.Day>= fromDate.Day) 
            && (e.EndDate.Value.Day <= toDate.Day )
            && (e.StartDate.Value.Year >= fromDate.Year)
            && (e.EndDate.Value.Year <= toDate.Year)
            && string.IsNullOrEmpty(eventName)?  eventName.Contains(e.EventName):   eventName.Contains(eventName));

      return query.ToList();
    }

 public JsonResult SearchEvents(string from,string to,int categoryId, string eventName)
 {
        DateTime frmDate= Convert.ToDateTime(from);
        DateTime toDate = Convert.ToDateTime(to);
        var list = _eventRepository.SearchEvents(frmDate,toDate,categoryId,eventName);  
        return Json(list, JsonRequestBehavior.AllowGet);
 }

i getting Error like:

Error :' A circular reference was detected while serializing an object of type 'CGWeb.Models.Repositories.Models.Event'.

how can solve this issue without removing virtual keyword?.please share..!

//

@Marc Gravell This is my Model

 [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; }
}

回答1:


This is nothing to do with the virtual keyword; it relates to the object graph. We can't see your graph, but the classic scenario here is a parent/child bidirectional relationship, i.e. where the parent has a .Children and the child has a .Parent.

A tree-serializer (such as xml, json, etc) will usually walk any members that are not explicitly marked to be ignored. Hence you would get an infinite loop as it went around that circle forever. Options:

  • use a non-cyclic DTO at this boundary (that is what I would do)
  • mark the offending back-reference for exclusion (the mechanism for this varies per serializer)


来源:https://stackoverflow.com/questions/7282185/errorserializing-entity-framework-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!