Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing a Web API project in C# that uses Entity Framework to pull data from a DB, serialize it and send it to a client.

My project has 2 classes, Post and Comment (foreign key from Post).

These are my classes.

Post class:

public partial class Post
{
    public Post()
    {
        this.Attachment = new HashSet<Attachment>();
        this.Comment = new HashSet<Comment>();
    }

    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public System.DateTime Created { get; set; }
    public Nullable<System.DateTime> Modified { get; set; }

    public virtual ICollection<Attachment> Attachment { get; set; }
    public virtual ICollection<Comment> Comment { get; set; }
}

Comment class:

public partial class Comment
{
    public int CommentId { get; set; }
    public string Content { get; set; }
    public System.DateTime Posted { get; set; }
    public bool Approved { get; set; }
    public int AnswersTo { get; set; }
    public int PostId { get; set; }

    public virtual Post Post { get; set; }
}

My problem is that when I try to get via Web API a Post, it spits me the following error:

Object graph for type 'APIServer.Models.Comment' contains cycles and cannot be serialized if reference tracking is disabled.

And when I try to get a Comment via Web API, the error is as follows:

Object graph for type 'System.Collections.Generic.HashSet`1[[APIServer.Models.Comment, APIServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'  contains cycles and cannot be serialized if reference tracking is disabled.

If I annotate the Comment class with

[DataContract(IsReference = true)]

the errors disappear, but the serialization only returns the ID of the comment and ignores the other fields.

Any suggestions on how to solve this?

Thanks in advance,

Léster

share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

Nevermind, annotating each property of Comment with

[DataMember]

shows the properties in the serialization. Though maybe there's a more elegant solution?

share|improve this answer
this is an answer? or comment? You need to consider using Dto to avoid this – Cuong Le Jul 18 at 8:21
It's an answer, though if there's a more correct solution, I'd like to know it. Did I post it in the wrong place? – Léster Jul 18 at 8:26
That's how DataContract works, Lester. Once you mark a type as DataContract, you need to mark all its members as DataMembers. You go from an implicit to an explicit model. – Youssef Moussaoui Jul 18 at 14:35
So it turns out I was doing only half the job, heh. Thanks for the explanation. – Léster Jul 20 at 16:44
add comment (requires an account with 50 reputation)

You can disable Lazy Loading on your Comment class by removing virtual from the Post property definition...

public partial class Comment
{
    public int CommentId { get; set; }
    public string Content { get; set; }
    public System.DateTime Posted { get; set; }
    public bool Approved { get; set; }
    public int AnswersTo { get; set; }
    public int PostId { get; set; }

    public Post Post { get; set; }
}

This should sort out the circular reference exception.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.