Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when serializing EF Code First 5.0 data in WebAPI Controller

I had originally asked this question: How Do I Resolve "A specified Include path is not valid"? which was answered, and my .Include() is now working, however, when the serializer tries to work it's magic, I get the following error:

You must write an attribute 'type'='object' after writing the attribute 
with local name '__type'.

Here's what I'm doing to return the data:

var everything = dc.Categories
            .Include(c => c.Products);

My class definitions are fairly straightforward:

public class Category
{
    public int CategoryId { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string Title { get; set; }

    public virtual Category Category { get; set; }
}

public class ProductDataContext : DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

I also tried removing 'virtual' but then I get circular references. I tried making the setter on ICollection Products private (as suggested here: http://forums.asp.net/t/1773164.aspx/1), which gets the error to clear, but then my products aren't part of the returned JSON.

What do I need to do to get the data to serialize with the categories and their products within?

EDIT Here was the stack trace I was getting:

[SerializationException: Object graph for type &#39;System.Collections.Generic.List`1[[Test.Models.Product, Test.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]&#39; contains cycles and cannot be serialized if reference tracking is disabled.]
System.Web.Http.WebHost.HttpControllerHandler.EndProcessRequest(IAsyncResult result) +30206
System.Web.Http.WebHost.HttpControllerHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +10
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9478661
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +178
like image 286
MisterJames Avatar asked Apr 06 '12 03:04

MisterJames


1 Answers

In order to fix this I needed to:

  1. Disable lazy loading, and
  2. Use the IgnoreDataMember from System.Runtime.Serialization as an attribute on the Category navigation property (the back-reference on the Product class).

Hope this helps someone.

To get around the XML-ish errors I used help from here: http://www.strathweb.com/2012/03/serializing-entity-framework-objects-to-json-in-asp-net-web-api/

To get around the problem with the cyclic references, I used this as a guide: MVC 4, Upshot entities cyclic references

like image 139
MisterJames Avatar answered Oct 17 '22 01:10

MisterJames