【问题标题】:Web Api Entity FrameworkWeb API 实体框架
【发布时间】:2018-04-10 22:04:57
【问题描述】:

我有一个错误:"ExceptionMessage": "Self referencing loop detected for property 'Posto' with type 'UfficioServer.Posto'. Path '[0].Dipendente[0]'.",

当我调用我的 web api 时,我需要一个与 Dipendente 关联的 Posti 列表...

 public List<Posto> GetAllPosti()
    {

        try
        {
            List<Posto> p = new List<Posto>();
            UfficioPostiEntities1 db = new UfficioPostiEntities1();
            db.Configuration.ProxyCreationEnabled = false;
            var posto = db.Posto.Include("Dipendente").ToList();
            var x = db.Posto.ToList();



            return x;
        }

有人可以帮助我吗?

【问题讨论】:

    标签: entity-framework asp.net-core-webapi


    【解决方案1】:

    为避免此错误,您应在 Global.asax 中的 Application_Start 中添加以下内容:

    HttpConfiguration config = GlobalConfiguration.Configuration;
    
    config.Formatters.JsonFormatter
            .SerializerSettings
            .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    

    这是因为(根据文档)

    ReferenceLoopHandling.Ignore:Json.NET 将忽略 引用循环而不是序列化它们。 第一次对象是 遇到它会像往常一样被序列化,但如果对象是 作为自身的子对象遇到的序列化程序将跳过 序列化它。

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2020-09-24
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多