title: Returning Entity Framework class from Web API throws "failed to serialize the response body for content type" exception
body: <p>I'm facing the following problem, I have an Object Model BlogEntry:</p>
  
  <pre><code>public class BlogEntry
      {
          public int Id { get; set; }
          [Required]
          public DateTime BlogEntryDate { get; set; }
          [Required]
          public string BlogEntryTitle { get; set; }
          [Required]
          public string BlogEntrySummary { get; set; }
          [Required]
          public string BlogEntryContent { get; set; }
          [Required]
          public string BlogEntryPublishedBy { get; set; }
          [Required]
          public bool BlogEntryPublished { get; set; }
          public int BlogCategoryId { get; set; }
          public virtual BlogCategory BlogCategory { get; set; }
      }
  </code></pre>
  
  <p>I generated the controller with the model and I have the method:</p>
  
  <pre><code> // GET: api/BlogEntries
          [AllowAnonymous]
          public IQueryable&lt;BlogEntry&gt; GetBlogEntries()
          {
              return db.BlogEntries;
          }
  </code></pre>
  
  <p>The problem is that when I test the method I have error in the serialization:</p>
  
  <blockquote>
    <p>the 'ObjectContent`1' type failed to serialize the response body for
    content type 'application/xml; charset=utf-8'</p>
  </blockquote>
  
  <p>I tried to fixed using the following code in the WebAPIConfig.cs:</p>
  
  <pre><code> config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
              config.Formatters.JsonFormatter
                          .SerializerSettings
                          .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
  </code></pre>
  
  <p>And actually this solve the problem locally, but when I publish the app to Azure keep giving me the error code 500.
  Note: I debug the method and I don't have a problem in the code, is very simple actually.</p>
  
  <p>Any idea please</p>
  
  <h2>Exception in the Browser</h2>
  
  <pre><code>&lt;Error&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
  &lt;StackTrace/&gt;
  &lt;InnerException&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  Type 'System.Data.Entity.DynamicProxies.BlogEntry_2BC5105C1EBC22432CBA0E820087939FDB4E152CC4026BA3BC6DDDD9C1156A4D' with data contract name 'BlogEntry_2BC5105C1EBC22432CBA0E820087939FDB4E152CC4026BA3BC6DDDD9C1156A4D:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer 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 the serializer.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;
  System.Runtime.Serialization.SerializationException
  &lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfBlogEntryToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.WebHost.HttpControllerHandler.&lt;WriteBufferedResponseContentAsync&gt;d__1b.MoveNext()
  &lt;/StackTrace&gt;
  &lt;/InnerException&gt;
  &lt;/Error&gt;
  </code></pre>