相关疑难解决方法(0)

JSON.NET错误检测到类型的自引用循环

我试图序列化从实体数据模型.edmx自动生成的POCO类,当我使用时

JsonConvert.SerializeObject 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误发生类型System.data.entity检测到的自引用循环.

我该如何解决这个问题?

serialization json json.net

458
推荐指数
9
解决办法
27万
查看次数

在WebApi 2中检测到属性的自引用循环

我创建了一个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)

c# asp.net-web-api postman

2
推荐指数
2
解决办法
5363
查看次数

Self referencing loop from Newtonsoft JsonSerializer using Entity Framework Core

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

1
推荐指数
1
解决办法
2755
查看次数