I'm trying to remove the inverse part of the relationship between VISITORDRIVER and USER when calling the API to get users. In this case VISITORDRIVER table has foreign key as USER table. Below is the JSON
[
{
"VISITORDRIVERs": [..],
"IDUSER": 1,
"USERNAME": "Mark",
"PASSWORD": "123456",
"USERID": "Mark"
}
]
Controller
public IQueryable<USER> GetUSERS()
{
return db.USERS;
}
Model
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; }
}
Thanks all
You can mitigate this problem in Global.asax.cs file by adding the following lines. This is called Circular dependency.
var jsonSerializeSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
jsonSerializeSettings.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.None;