I originally asked this question: How do I resolve it? The specified Include path is invalid "? , Which was answered, and my .Include () now works, however, when the serializer tries to work with magic, I get the following error:
You must write an attribute 'type'='object' after writing the attribute with local name '__type'.
Here is what I do to get the data back:
var everything = dc.Categories .Include(c => c.Products);
My class definitions are pretty simple:
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 links. I tried to make the setter on ICollection Products private (as suggested here: http://forums.asp.net/t/1773164.aspx/1 ), which gets an error to clear, but then my products are not part of the returned JSON.
What do I need to do to get serialization data with categories and their products inside?
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
asp.net-web-api entity-framework-5 ef-code-first
Misterjames
source share