1つの投稿が複数のpostCommentsを持つことができるという関係を持とうとすると、以下のこのエラーが発生します
System.Collections.Generic.List`1[[DAO.Models.PostComment, DAO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 型のオブジェクトのシリアル化中に循環参照が検出されました。
int userId = (int)Session["UserId"];
try
{
IEnumerable<Post> userPosts;
userPosts = (from q in db.Posts
where q.UserId == userId
&& q.PostId > postid
select q).Take(5).ToList();
return Json(userPosts.Select(x => new
{
success = 1,
contenttext = x.PostContent,
postId = x.PostId,
comments = x.PostComments
}), JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { success = 0 });
}
finally
{
//db.Dispose();
}
私の投稿クラス
public partial class Post
{
public Post()
{
this.PostComments = new List<PostComment>();
}
public int UserId { get; set; }
public int PostId { get; set; }
public string PostContent { get; set; }
public virtual ICollection<PostComment> PostComments { get; set; }
public partial class PostComment
{
public long PostCommentID { get; set; }
public int PostId { get; set; }
public int ParentCommentID { get; set; }
public System.DateTime CommentDate { get; set; }
public string Comment { get; set; }
public virtual Post Post { get; set; }
}
}