title: ASP.NET MVC4 Web API Controller serialization
body: <p>I am trying to create a RESTful web service that returns a list of products using ASP.NET MVC4 Web API. Here is my controller class</p>
  
  <pre><code>public class ProductController : ApiController
  {
      public IEnumerable&lt;Product&gt; GetProducts()
      {
          WebCatalogContext dbcontext = DatabaseConfig.Instance.Context;
          List&lt;Product&gt; plist = dbcontext.Products.ToList();
          return plist;
      }
  }
  </code></pre>
  
  <p>When I run my service and call the following URL from my browser :/api/Product, I get System.Runtime.Serialization.SerializationException. I looked into my plist object and there is no problem with it.</p>
  
  <p>Here is my data model:</p>
  
  <pre><code>[DataContract(Name = "p")]
  [Serializable]    
  public class Product
  {
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      [DataMember(Name = "id")]
      public int Id { get; set; } 
  
      [Required, MaxLength(50)]
      [DataMember(Name = "ti")]
      public string Title { get; set; }
  
      [Required]
      [DataMember(Name = "de")]
      public string Description { get; set; }
  
      [Required]
      [DataMember(Name = "ph")]
      public string PhotoURL { get; set; }
  
      [DataMember(Name = "ca")]        
      public virtual ProductCategory Category { get; set; }
  }
  
  
  [DataContract(Name="pc")]
  [Serializable]
  public class ProductCategory
  {
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      [DataMember(Name="id")]
      public int Id { get; set; }
  
      [DataMember(Name="nm")]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>When I remove the reference to ProductCategory from my Product class, all things work just fine. But, when I include it I get the following exception. </p>
  
  <p><code>Type 'System.Data.Entity.DynamicProxies.Product_664E9A0AA1F165A26C342B508BFFF1279FD3FE059285225BDA19F407A29A9CAD' with data contract name 'Product_664E9A0AA1F165A26C342B508BFFF1279FD3FE059285225BDA19F407A29A9CAD:<a href="http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies" rel="nofollow">http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies</a>' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.</code></p>
  
  <p>Any idea about what I am missing?</p>
  
  <p>Regards</p>