Błąd podczas serializowania danych EF Code First 5.0 w kontrolerze WebAPI
Pierwotnie zadałem to pytanie:Jak rozwiązać problem „Określona ścieżka Uwzględnij jest nieprawidłowa”? który został odebrany, a mój .Include () już działa, jednak gdy serializator próbuje działać, to magia, otrzymuję następujący błąd:
<code>You must write an attribute 'type'='object' after writing the attribute with local name '__type'. </code>
Oto co robię, aby zwrócić dane:
<code>var everything = dc.Categories .Include(c => c.Products); </code>
Moje definicje klas są dość proste:
<code>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; } } </code>
Próbowałem również usunąć „wirtualne”, ale potem otrzymuję odwołania cykliczne. Próbowałem ustawić seter na produktach ICollection prywatnie (jak sugerowano tutaj:http://forums.asp.net/t/1773164.aspx/1), który usuwa błąd, ale wtedy moje produkty nie są częścią zwróconego JSON.
Co muszę zrobić, aby dane serializować za pomocą kategorii i ich produktów?
EDYTOWAĆ Oto ślad stosu, który otrzymywałem:
<code>[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 </code>