1

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.

4
  • Most likely nobody has any idea what you're doing wrong, you have not provided any code to that produces an exception. How to create a Minimal, Complete and Verifiable example. Commented Mar 2, 2017 at 18:21
  • Instead of adding the found solution to the question, accept Yashveer's answer. Commented Mar 2, 2017 at 18:51
  • Now that I found the solution , I came with another question, Do I have to ask another question or append it to this? Commented Mar 2, 2017 at 18:54
  • You should ask a new question. Commented Mar 2, 2017 at 19:09

2 Answers 2

3
try this on startup .cs 

 config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling 
= Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks I resolve the issue with: GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; Which is basically the same thing
@AngelSilva great
Though this solves your issue, consider using DTO's instead.
0

Try this:

JsonSerializerSettings settings = new JsonSerializerSettings(); 

settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

 var Serializedata =JsonConvert.SerializeObject(data,settings);

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.