错误:序列化实体框架类 [英] Error:serializing Entity Framework class

查看:155
本文介绍了错误:序列化实体框架类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公开的IList<事件> SearchEvents(DateTime的寄件者,日期TODATE,诠释的categoryId,串eventName的)
{

VAR的查询= context.Events.Include(城市),其中(E =方式> e.EventCategoryID ==(&的categoryId LT = 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。 ?年)
和;&安培; string.IsNullOrEmpty(eventName的)eventName.Contains(e.EventName):eventName.Contains(eventName的));

返回query.ToList();
}

公共JsonResult SearchEvents(从字符串,字符串,整数的categoryId,串eventName的)
{
的DateTime frmDate = Convert.ToDateTime(从);
的DateTime TODATE = Convert.ToDateTime(地);
无功名单= _eventRepository.SearchEvents(frmDate,TODATE,的categoryId,eventName的);
返回JSON(列表,JsonRequestBehavior.AllowGet);
}



我收到错误,如:

 错误:CGWeb.Models.Repositories.Models.Event'',而序列化类型的对象时检测到循环引用。 



如何解决这个问题,而不移除虚拟关键字?请您及时分享..!



//



@Marc Gravell这是我的模型

  [表(Table_Events)] 
公共部分类事件
{
[关键]
公众诠释ID {搞定;组; }

//公众诠释? LocationId {搞定;组; }
//公众诠释? ImageId {搞定;组; }
公共字符串事件名称{搞定;组; }

[NotMapped]
公共字符串EventAddress {搞定;组; }

公共字符串EventUrl {搞定;组; }
公共字符串EventDesc {搞定;组; }
公众可空<&System.DateTime的GT;起始日期{搞定;组; }
公众可空<&System.DateTime的GT;结束日期{搞定;组; }

公众可空< INT> EventCategoryID {搞定;组; }
公众诠释CityID {搞定;组; }
公众诠释看过{搞定;组; }

[ForeignKey的(EventCategoryID)]
公共虚拟EventCategory EventCategory {搞定;组; }
// [ForeignKey的(ImageId)]
[NotMapped]
公共虚拟ImageViewModel图片{搞定;组; }

// [ForeignKey的(LocationId)]
//公共虚拟地点{搞定;组; }


[ForeignKey的(CityID)]
公共虚拟城市城市{搞定;组; }

[NotMapped]
公共BOOL ISSponsorship {搞定;组; }

[NotMapped]
公共管理组织者{搞定;组; }

// [NotMapped]
[ForeignKey的(事件ID)]
公共虚拟的IList<主治>主治{搞定;组; }
}


解决方案

这是无关与虚拟关键字;它涉及到对象图。我们无法看到您的图形,但是的经典的情况下这里是一个父/子双向关系,即如果母公司有一个。孩子和孩子有一个 .Parent



一个树串器(如XML,JSON等)通常会走路没有明确标记为被忽略任何成员。因此,它绕到这个圈子永远,你会得到一个无限循环。选项:




  • 在这个边界使用非循环DTO(这就是我会做)

  • 标记违规排除反向引用(此机制每串而异)


    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)

这篇关于错误:序列化实体框架类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆