4

与多对多关系有一些重大问题。

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; }
}

像这样返回新添加的团队后

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;  

我在响应内部异常中收到以下错误:

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

显然有一个循环引用正在进行,但我希望一个团队能够拥有许多用户,而一个用户能够拥有许多团队。有没有办法在不创建 DTO 的情况下解决这个问题?

4

0 回答 0