开发者

Error:serializing Entity Framework class

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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜