title: NHibernate + Multiple foreign keys to same column
body: <p>I'm trying to map my <code>Team</code> and <code>Match</code> table by using following mappings:</p>
  
  <p>// Team.cs</p>
  
  <pre><code>public class Team
  {
      public virtual int ID { get; private set; }
      public virtual string TeamName { get; set; }
      public virtual Cup Cup { get; set; }
      public virtual IList&lt;Match&gt; Matches { get; set; }
  
      public Team()
      {
          Matches = new List&lt;Match&gt;();
      }
  }
  
  public class TeamMap : ClassMap&lt;Team&gt;
  {
      public TeamMap()
      {
          Id(x =&gt; x.ID);
          Map(x =&gt; x.TeamName).Not.Nullable();
          References(x =&gt; x.Cup, "CupID");
  
          HasMany(x =&gt; x.Matches)
              .Key(x =&gt; x.Columns.Add("Team1ID", "Team2ID"))
              .Inverse().Cascade.AllDeleteOrphan();
  
          Table("Teams");
      }
  }
  </code></pre>
  
  <p>// Match.cs</p>
  
  <pre><code>public class Match
  {
      public virtual int ID { get; private set; }
      public virtual Team Team1 { get; set; }
      public virtual Team Team2 { get; set; }
      public virtual int WinnerID { get; set; }
      public virtual Cup Cup { get; set; }
  }
  
  public class MatchMap : ClassMap&lt;Match&gt;
  {
      public MatchMap()
      {
          Id(x =&gt; x.ID);
          Map(x =&gt; x.WinnerID);
          References(x =&gt; x.Team1, "Team1ID");
          References(x =&gt; x.Team2, "Team2ID");
          References(x =&gt; x.Cup, "CupID");
          Table("Matches");
      }
  }
  </code></pre>
  
  <p>However, it throws an exception that says:</p>
  
  <blockquote>
    <p>Foreign key
    (FKEFFCA4CA45169AED:Matches [Team1ID,
    Team2ID])) must have same number of
    columns as the referenced primary key
    (Teams [ID])</p>
  </blockquote>
  
  <p>Any suggestions?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I was able to solve it by mixing something together based on the comment wrote by @Yads.</p>
  
  <p>My code:</p>
  
  <p>// Team.cs</p>
  
  <pre><code>public class Team
  {
      public virtual int ID { get; private set; }
      public virtual string TeamName { get; set; }
      public virtual Cup Cup { get; set; }
      public virtual IList&lt;Match&gt; HomeMatches { get; set; }
      public virtual IList&lt;Match&gt; AwayMatches { get; set; }
      public virtual IList&lt;Match&gt; Matches { get { return HomeMatches.Concat(AwayMatches).ToList(); }}
  
      public Team()
      {
          HomeMatches = new List&lt;Match&gt;();
          AwayMatches = new List&lt;Match&gt;();
      }
  }
  
  public class TeamMap : ClassMap&lt;Team&gt;
  {
      public TeamMap()
      {
          Id(x =&gt; x.ID);
          Map(x =&gt; x.TeamName).Not.Nullable();
          References(x =&gt; x.Cup, "CupID");
  
          HasMany(x =&gt; x.HomeMatches).KeyColumn("HomeTeamID")
              .Inverse().Cascade.AllDeleteOrphan();
  
          HasMany(x =&gt; x.AwayMatches).KeyColumn("AwayTeamID")
              .Inverse().Cascade.AllDeleteOrphan();
          Table("Teams");
      }
  }
  </code></pre>
  
  <p>// Match.cs</p>
  
  <pre><code>public class Match
  {
      public virtual int ID { get; private set; }
      public virtual Team HomeTeam { get; set; }
      public virtual Team AwayTeam { get; set; }
      public virtual int WinnerID { get; set; }
      public virtual Cup Cup { get; set; }
  }
  
  public class MatchMap : ClassMap&lt;Match&gt;
  {
      public MatchMap()
      {
          Id(x =&gt; x.ID);
          Map(x =&gt; x.WinnerID);
          References(x =&gt; x.HomeTeam, "HomeTeamID");
          References(x =&gt; x.AwayTeam, "AwayTeamID");
          References(x =&gt; x.Cup, "CupID");
          Table("Matches");
      }
  }
  </code></pre>
  
  <p>However, I'm not aware of what drawbacks this approach have... The .Concat() seems a bit nasty to me...</p>