c# - 包含循环,如果引用跟踪被禁用,则无法序列化,json.net 和 webapi

标签 c# entity-framework ef-code-first asp.net-web-api entity-framework-5

我收到错误:

 Object graph for type 'System.Collections.Generic.List`1[[Proj.Model.Prom, Proj.Model, 
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be 
 serialized if reference tracking is disabled.

阅读有关内容,似乎是序列化程序,但 Json.Net 声称是解决方案,我已经阅读了 WebApi 和 Framework 4.5 默认情况下具有它。那么它是默认出现的吗?如果是这样,为什么我仍然收到该错误?

谢谢!吉列尔莫。

编辑:添加代码

using System;
using System.Collections.Generic;
using System.Data.Spatial;

namespace Proj.Model
{
    public class Prom
    {
        public Prom()
        {
            this.Stores = new List<Store>();
            this.Branches = new List<Branch>();
            this.Products = new List<Product>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public DbGeography Location { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public int StateId { get; set; }
        public int CategoryId { get; set; }

        public virtual ICollection<Store> Stores { get; set; }
        public virtual ICollection<Branch> Branches { get; set; }
        public virtual ICollection<Product> Products { get; set; }

        public virtual Category Category { get; set; }
        public virtual State  State  { get; set; }

    }
}

using System;
using System.Collections.Generic;

namespace Proj.Model
{
    public class Category
    {
        public Category()
        {
            this.Proms = new List<Prom>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Prom> Proms { get; set; }
    }
}

然后运行类似这样的东西会返回错误

public IEnumerable<Category> GetList(int estadoId, string idTiposTarjetasList)
{
    var ids = "1,2,3,4".Split(',');
    var intIds = ids.Select(int.Parse);

    var Categories = Uow.Categorias.GetAllIncluding(c => c.Proms).ToList();
    foreach (var category in Categories)
    {
        var proms = category.Proms.Where(p => intIds.Contains(p.Id) && p.StateId == stateId).ToList();
        category.Proms = proms;
    }
    return Categories
}

最佳答案

默认情况下,WebApi 将“PreserveReferencesHandling”设置为 None。

您可以在 WebApiConfig.cs 中配置 Json.NET SerializerSettings:

config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = 
    Newtonsoft.Json.PreserveReferencesHandling.All;

关于c# - 包含循环,如果引用跟踪被禁用,则无法序列化,json.net 和 webapi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13480760/

相关文章:

c# - Entity Framework 中的级联更新

entity-framework - EF Code First 配置中的多级继承

asp.net-mvc-3 - MvcScaffold 没有正确创建我的相关下拉列表

c# - 选择器标题颜色不可更改

c# - 来自 Model 的完整 Java/C# 代码生成工具?

c# - Access 数据库引擎的优点和缺点。 SQLite 之后的生活

c# - EntityFramework Core - 复制实体并将其放回数据库

c# - Entity Framework 6 上的数据库迁移问题

c# - EF Code First : Duplicate foreign keys (one from name convention, 来自导航属性)

c# - 为什么在混合 C# ValueTuple 和动态时出现此编译器错误