Tuesday, May 31, 2011

Entity Framework Serialization - Circular Reference

I have been working with KnockoutJS recently and needed to serialize an Entity Framework model to the client as JavaScript so that I could convert it to my Knockout ViewModel. Here is the entity that I needed to serialize.



However, when I attempted to serialize this object, I was getting an error "A circular reference was detected while serializing and object of type Person". I did a little research on this issue and found a good blog post by Rick Strahl on LINQ to SQL and Serialization, that discusses this issue.

The problem is the public Tags property. The first suggestion is to change the access modifier from public to internal. I did this and it worked great. I have not fully explored the ramifications of making this change to the rest of my application, but will post an update if I find anything.