Buginator
Buginator

Reputation: 846

Code First CTP4: How to map these two entities

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.

Upvotes: 0

Views: 151

Answers (1)

Alejandro Martin
Alejandro Martin

Reputation: 5877

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

Upvotes: 0

Related Questions