title: Entity Framework Code First with one class having two references to another
body: <p>I love EF Code First, but sometimes it seems it would be easier to just define the tables in SQL.</p>
  
  <p>In this case I have two models, something like this:</p>
  
  <pre><code>public class Book
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;BookRecommendation&gt; BookRecommendations { get; set; }
      // additional properties about the item
  }
  
  public class BookRecommendation
  {
      public int Id { get; set; }
      // public int BookId { get; set; } // from attempts to use data annotations
      public virtual Book Book { get; set; }
      // public int BookRecommendedId { get; set; } // from attempts to use data annotations
      public virtual Book BookRecommended { get; set; }
      // additional properties about the relationship between the two items
  }
  </code></pre>
  
  <p>Unfortunately, I can't get this to work correctly with either data annotations or the fluent API.</p>
  
  <p>There are questions like <a href="https://stackoverflow.com/q/6015964/11912">Multiple foreign keys pointing to same table in Entity Framework 4.1 code first</a>, <a href="https://stackoverflow.com/q/5828394/11912">Entity Framework 4.1 InverseProperty Attribute and ForeignKey</a>, and others like this, but these tend to involve collections on both end.</p>
  
  <p>My models could just be wrong, since frustration set in early and I thought about how I'd do this in SQL:</p>
  
  <pre><code>Book
      Id
      Name
      // other properties
  
  Recommendation
      Id
      BookId
      RecommendedBookId
      // other properties
  </code></pre>
  
  <p>There would then be foreign keys between <code>Book.Id : Recommendation.BookId</code> and <code>Book.Id : Recommendation.RecommendedBookId</code>.</p>
  
  <p>What do I need to do via data annotations or fluent API to get this working, or how should I modify my models?</p>