I'm currently using WebApi 2.0 and EntityFrameWork and I'm having issues with it:
Self referencing loop detected for property 'UserInfo' with type 'System.Data.Entity.DynamicProxies.UserInfo_F7C6DF3909A804C5A9AC107297C8851F4CC9DF1CCA4A689B892B6C6EBA5A6EA8'. Path '[0].User'."
my DB is something like this: User-UserInfo and is 1-1 relationship, the PK of User is the PF in UserInfo;
public class User {
public int UserId { set; get; }
public string username { set; get; }
public string password { set; get; }
public string name { set; get; }
public string email { set; get; }
public string surname { set; get; }
public string lastName { set; get; }
public int age { set; get; }
public DateTime regDate { set; get; }
public bool userType { set; get; }
//we define our relationships
//1-1 UserModel-UserInfo
public virtual UserInfo UserInfo { set; get; }
}
My UserInfo class:
public class UserInfo {
[Key, ForeignKey("User")]
public int UserId { set; get; }
public string username { set; get; }
public string phone{ set; get; }
public string adress { set; get; }
public string country { set; get; }
public string city { set; get; }
public string zip { set; get; }
//we define our relationships
//1-1 UserModel-UserInfo
public virtual User User { set; get; }
}
In Postman I send a Post request and register a user:
{
"username": "otmanlicona",
"password": "pwd1234",
"name": "Otman",
"email": "[email protected]",
"surname": "licona",
"lastName": "ledezma",
"age": 33,
"regDate": "2017-03-01T18:10:11+00:00",
"userType": false
}
If I send a GET request I get all the users:
[
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": null,
"UserId": 7,
"username": "otmanlicona",
"password": "pwd1234",
"name": "Otman",
"email": "[email protected]",
"surname": "licona",
"lastName": "ledezma",
"age": 33,
"regDate": "2017-03-01T12:10:11",
"userType": false
},
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": null,
"UserId": 8,
"username": "angelsilva",
"password": "pwd1234",
"name": "angel",
"email": "[email protected]",
"surname": "silva",
"lastName": "borja",
"age": 22,
"regDate": "2017-03-01T12:10:11",
"userType": true
}
]
No problems so far , the problem is when I insert the UserInfo:
{
"UserId": 8,
"username": "angelsilva",
"phone": "12345678",
"adress": "550 Swallow Hill",
"country": "USA",
"city": "foo",
"zip": "47-253"
}
If I send another GET request I get an Exception: Image of the error
Self referencing loop detected for property 'UserInfo'
Can anyone tell me what I'm doing wrong?,Thanks
[
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": null,
"UserId": 7,
"username": "otmanlicona",
"password": "pwd1234",
"name": "Otman",
"email": "[email protected]",
"surname": "licona",
"lastName": "ledezma",
"age": 33,
"regDate": "2017-03-01T12:10:11",
"userType": false
},
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": {
"UserId": 8,
"username": "angelsilva",
"phone": "12345678",
"adress": "550 Swallow Hill",
"country": "USA",
"city": "foo",
"zip": "47-253"
},
"UserId": 8,
"username": "angelsilva",
"password": "pwd1234",
"name": "angel",
"email": "[email protected]",
"surname": "silva",
"lastName": "borja",
"age": 22,
"regDate": "2017-03-01T12:10:11",
"userType": true
}
]
NOW I see what does Entity Framework meant by self referencing loop; What can I do so in my Get response only returns me the other values that aren't in the User Class like username,phone,address,country, etc.
try this on startup .cs
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;