我试图序列化从实体数据模型.edmx自动生成的POCO类,当我使用时
JsonConvert.SerializeObject
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误发生类型System.data.entity检测到的自引用循环.
我该如何解决这个问题?
我创建了一个Web Api来保存数据库中的新产品和评论.下面是WebApi代码:
// POST api/Products
[ResponseType(typeof(Product))]
public IHttpActionResult PostProduct(Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Products.Add(product);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = product.ProductId }, product);
}
Run Code Online (Sandbox Code Playgroud)
产品类别 -
public class Product
{
public int ProductId { get; set; }
[Required]
public string Name { get; set; }
public string Category { get; set; }
public int Price { get; set; }
//Navigation Property
public ICollection<Review> Reviews { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
复习课程 -
public class Review
{ …Run Code Online (Sandbox Code Playgroud) I've encountered the error:
JsonSerializationException: Self referencing loop detected for property 'Subject' with type 'Project.Models.Subject'. Path 'data[0].Totals'.
It occurs when I load a View with a dataGrid populated by an IEnumerable<Subject> model. The Grid is a DevExtreme DataGrid bound to the View's model like this:
@(Html.DevExtreme().DataGrid()
.DataSource(Model)
.Paging(paging =>
{
paging.Enabled(true);
paging.PageIndex(0);
paging.PageSize(20);
})
.Columns(columns =>
{
columns.Add().DataField("SubjectId");
... other fields
})
)
Run Code Online (Sandbox Code Playgroud)
Which is populated from a Controller that pulls data from a Repository with this function:
public async …Run Code Online (Sandbox Code Playgroud) c# json.net devextreme entity-framework-core asp.net-core-mvc