4

Having som major problem with many to many relation.

public class Team
{
    public Team()
    {
        Users = new HashSet<User>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<User> Users { get; set; }
}

public class User
{
    public User()
    {
        Teams = new HashSet<Team>();
    }

    public int Id { get; set; }
    public string Username { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Cell { get; set; }

    public ICollection<Team> Teams { get; set; }
}

After Returning a newly added Team like this

var currentUser = _ctx.Users.Where(u => u.Username ==    HttpContext.Current.User.Identity.Name).SingleOrDefault();

        teamToAdd.Users.Add(currentUser);
        var teamAdded = _ctx.Teams.Add(teamToAdd);
        Save();

        return teamAdded;  

I get the following error in the response inner exception:

"Self referencing loop detected with type 'MatchMaker.Data.Team'. Path 'Users[0].Teams'."

There is obviously a circular reference going on but i want a Team to be able to have many Users and a User to be able to have many Teams. Is there any way of getting past this withouth creating DTOs?

limlim
  • 395
  • 1
  • 6
  • 14
  • possible duplicate of [JSON.NET Error Self referencing loop detected for type](http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type) – Eric Hotinger Aug 09 '13 at 14:02
  • 4
    config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; in the WebApiConfig.cs did the trick – limlim Aug 09 '13 at 14:34
  • Cool, glad to see that worked. – Eric Hotinger Aug 09 '13 at 15:08
  • Is it possible not to load teams? – eugenekgn Jan 08 '15 at 19:40
  • Possible duplicate of [Entity framework self referencing loop detected](http://stackoverflow.com/questions/19467673/entity-framework-self-referencing-loop-detected) – Paul Sweatte Oct 21 '15 at 12:41

0 Answers0