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 'System.Collections.Generic.List`1[[Test.Models.Product, Test.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 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& completedSynchronously) +178
In order to fix this I needed to:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With