title: Serializing EF4.1 Entities using JSON.Net
body: <p>I am building an application using MVC3, Razor view engine, Repository Pattern with Unit of Work and using EF4.1 Code First to define my data model.</p>
  
  <p>Here is a bit of background (gloss over it if you want).</p>
  
  <p>The application itself is just an Intranet 'Menu'.</p>
  
  <p>The 2 main entities are MenuItem and Department of which:</p>
  
  <ul>
  <li>MenuItem can have many Departments</li>
  <li>Departments can have many MenuItems</li>
  <li>MenuItem may have a MenuItem as a parent</li>
  </ul>
  
  <p>This is how I have defined my Entities</p>
  
  <pre><code>public class MenuItem
  {
     public int MenuItemId { get; set; }
     public string Name { get; set; }
     public string Url { get; set; }
     public virtual ICollection&lt;Department&gt; Departments { get; set; }
     public int? ParentId { get; set; }
     public virtual MenuItem ParentMenuItem { get; set; }
  }
  
  public class Department
  {
     public int DepartmentId { get; set; }
     public string Name { get; set; }
     public virtual ICollection&lt;MenuItem&gt; MenuItems { get; set; }
  }
  </code></pre>
  
  <p>I am using the FluentAPI to define the Self Reference Many-to-Many for the MenuItem.</p>
  
  <p>The issue I am having is passing a MenuItem to the view via JSON.
  The central issues are that I have a circular reference between my entities that the built in JSON parser can't deal with and I have lazy loading and proxy generation still enabled.</p>
  
  <p>I am using JSON.net library from Nuget as my JSON Serializer as this seems to be a nice way round the circular reference issue. I now am unsure how to 'fix' the proxy generation issue. Currently the serializer throws <code>The RelationshipManager object could not be serialized. This type of object cannot be serialized when the RelationshipManager belongs to an entity object that does not implement IEntityWithRelationships.</code></p>
  
  <p>Can anyone help me with this? If I turn off proxy generation, I am going to have a hell of a time loading all of the MenuItem children so I am keen leave this on. I have read a fair amount and there seems to be a variety of different answers including projecting the entities into another object and serialize that, etc, etc. Ideally there would be some way of configuring JSON.net to ignore the RelationshipManager object?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Here is what I have used as a Custom ContractResolver for JSON.Net serializer. This seems to have sorted out my issue.</p>
  
  <pre><code>public class ContractResolver : DefaultContractResolver
  {
      private static readonly IEnumerable&lt;Type&gt; Types = GetEntityTypes();
      private static IEnumerable&lt;Type&gt; GetEntityTypes()
      {
          var assembly = Assembly.GetAssembly(typeof (IEntity));
          var types = assembly.GetTypes().Where(t =&gt; String.Equals(t.Namespace, "Namespace", StringComparison.Ordinal));
          return types;
      }
  
      protected override List&lt;MemberInfo&gt; GetSerializableMembers(Type objectType)
      {
          if (!AllowType(objectType))
              return new List&lt;MemberInfo&gt;();
  
          var members = base.GetSerializableMembers(objectType);
          members.RemoveAll(memberInfo =&gt; (IsMemberEntityWrapper(memberInfo)));
          return members;
      }
  
      private static bool AllowType(Type objectType)
      {
          return Types.Contains(objectType) || Types.Contains(objectType.BaseType);
      }
  
      private static bool IsMemberEntityWrapper(MemberInfo memberInfo)
      {
          return memberInfo.Name == "_entityWrapper";
      }
  }
  </code></pre>
  
  <p><code>IEntity</code> is an interface all my Code First entity objects implement.</p>