views:

27

answers:

1

Let's say that I have two entities, Team and Match. In every Match, there are two teams, the HomeTeam and the AwayTeam. A Team can have many matches, but HomeTeam and AwayTeam can only have one team each. Sometimes Team is a HomeTeam, and sometimes the same Team is an AwayTeam. I have provided just the basics for each of the classes:

public class Team
{
    public int TeamId { get; set; }
    public string Name { get; set; }
}



public class Match
{
        public int MatchId { get; set; }
        public int HomeTeamId { get; set; }
        public int AwayTeamId { get; set; }
}

How can I map this? I tried (with setting ICollection Matches and tried to map it, but I got that HomeTeam and AwayTeam can't have the same inverse relationship (something like that).

Thanks.

A: 

How about this?

public class Team
{
    public int TeamId { get; set; }
    public string Name { get; set; }
    public List<Match> Matches {get; set;}
}

public class Match
{
    public int MatchId { get; set; }
    public Team HomeTeamId { get; set; }
    public Team AwayTeamId { get; set; }
}
Alejandro Martin
Thanks. Didn't work though, I still get: Navigation property 'Matches' of 'MyProject.Team' cannot be the inverse of both navigation properties 'AwayTeam' and 'HomeTeam' of 'MyProject.Match'. In case you dont know, I'm trying to use the relatively new Code First approach (which is yet to be RTM by Microsoft) in combination with Entity Framework.
Buginator
Alejandro Martin