Mis*_*mes 7 ef-code-first asp.net-web-api entity-framework-5
我最初问过这个问题: 如何解决"指定的包含路径无效"?已经回答了,而我的.Include()现在正在工作,但是,当序列化程序试图使它变得神奇时,我收到以下错误:
You must write an attribute 'type'='object' after writing the attribute
with local name '__type'.
Run Code Online (Sandbox Code Playgroud)
这是我正在做的返回数据:
var everything = dc.Categories
.Include(c => c.Products);
Run Code Online (Sandbox Code Playgroud)
我的类定义相当简单:
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; }
}
Run Code Online (Sandbox Code Playgroud)
我也尝试删除'虚拟',但后来我得到循环引用.我尝试将ICollection Products上的setter设为私有(如此处所示:http://forums.asp.net/t/1773164.aspx/1),这样可以清除错误,但之后我的产品不属于返回JSON.
我需要做些什么来使数据与类别及其产品序列化?
编辑 这是我得到的堆栈跟踪:
[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
Run Code Online (Sandbox Code Playgroud)
Mis*_*mes 13
为了解决这个问题,我需要:
IgnoreDataMemberfrom System.Runtime.Serialization作为Category属性(类的后引用Product).希望这有助于某人.
为了解决XML-ish错误,我从这里使用了帮助:http: //www.strathweb.com/2012/03/serializing-entity-framework-objects-to-json-in-asp-net-web-api/
为了解决循环引用的问题,我使用它作为指南: MVC 4,Upshot实体循环引用
| 归档时间: |
|
| 查看次数: |
6788 次 |
| 最近记录: |