【问题标题】:remove inverse relationship in ASP.NET Web API 2 / MVC 5删除 ASP.NET Web API 2 / MVC 5 中的反向关系
【发布时间】:2021-09-09 22:30:39
【问题描述】:

在调用 API 获取用户时,我正在尝试删除 VISITORDRIVER 和 USER 之间关系的反向部分。在这种情况下,VISITORDRIVER 表具有外键作为 USER 表。下面是 JSON

[
  {
    "VISITORDRIVERs": [..],
    "IDUSER": 1,
    "USERNAME": "Mark",
    "PASSWORD": "123456",
    "USERID": "Mark"
  }
]

控制器

public IQueryable<USER> GetUSERS()
{
    return db.USERS;
}

型号

public partial class USER
{

    public USER()
    {
        this.VISITORDRIVERs = new HashSet<VISITORDRIVER>();
    }

    public int IDUSER { get; set; }
    public string USERNAME { get; set; }
    public string PASSWORD { get; set; }
    public string USERID { get; set; }

   
    public virtual ICollection<VISITORDRIVER> VISITORDRIVERs { get; set; }
}

谢谢大家

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-web-api2


【解决方案1】:

您可以通过在 Global.asax.cs 文件中添加以下行来缓解此问题。这称为循环依赖。

       var jsonSerializeSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        jsonSerializeSettings.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.None;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-04
    • 2015-03-23
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2015-01-01
    相关资源
    最近更新 更多