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?