Forgive me if this is a duplicate as I am not sure exactly how to frame the question/search.
I have two MS SQL tables, match and team.
Match
- matchId (PK int)
- homeTeamId (FK int)
- awayTeamId (FK int)
Team
- teamId (PK int)
- Name (nvarchar)
I also have two vb.net classes 'Match' and 'Team'.
Public Class Match
Public Property MatchId As Integer
Public Property HomeTeamId As Integer
Public Property AwayTeamId As Integer
Public Overridable Property AwayTeam As Team
Public Overridable Property HomeTeam As Team
End Class
Public Class Team
Public Property TeamId As Integer
Public Property Name As String
End Class
I have two relationships set up where Team.TeamId links to both Match.HomeTeamId and Match.AwayTeamId.
When I run my app, I get an exception (innerException) that says
{"Invalid column name 'AwayTeam_TeamId'.
Invalid column name 'HomeTeam_TeamId'.
Invalid column name 'Team_TeamId'."}
I have tried to use the fluent API to map the relationships, but I am afraid that I am not exactly sure how to set this up so that I can get both properties to properly map to the properties in Match.
Using the Fluent API, I have been able to map one property (HomeTeam), while commenting out the 'Match.AwayTeam' property, but not both.
modelBuilder.Entity(Of Match)().HasRequired(Function(m) m.HomeTeam).WithMany(Function(t) t.Matches).HasForeignKey(Function(m) m.HomeTeamId)
How should I set this up so that the Match class contains both the HomeTeam and AwayTeam properties?
Update
I managed to get this working with the following code in the 'OnModelCreating' method of my context.
Thanks to @Slauma for the correction regarding 'WithMany'.
modelBuilder.Entity(Of Match).HasRequired(Function(m) m.HomeTeam).WithMany().Map(Function(t) t.MapKey("HomeTeamId"))
modelBuilder.Entity(Of Match).HasRequired(Function(m) m.AwayTeam).WithMany().Map(Function(t) t.MapKey("AwayTeamId"))