This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.ObjectModel; | |
using System.Collections.Generic; | |
namespace MyTest | |
{ | |
public class Person | |
{ | |
public Person() | |
{ | |
Tags = new Collection<Tag>(); | |
} | |
public int Id { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public ICollection<Tag> Tags { get; set; } | |
} | |
public class Tag | |
{ | |
public Tag() | |
{ | |
People = new Collection<Person>(); | |
} | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public ICollection<Person> People { get; set; } | |
} | |
} |
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.