title: Creating a double linked list in Entity Framework
body: <p>I have an object which can optionally have a reference to a  next and/or previous record. Something like this:</p>
  
  <pre><code>public class Foo
  {
    [Key]
    public int Id {get; set;}
  
    [ForeignKey("Previous")]
    public int? PreviousId {get; set;}
  
    public Foo Previous {get; set;}
  
    [InverseProperty("Previous")]
    public Foo Next {get; set;}
  }
  </code></pre>
  
  <p>Unfortunately this does not work, instead resulting in the error message <code>Unable to determine the principal end of an association between the types Foo and Foo</code>. </p>
  
  <p>The idea is that by setting the <code>PreviousId</code>, the Previous <code>Foo</code> will get its <code>Next</code> set automatically by EF. This is to prevent errors caused by <code>Next</code> and <code>Previous</code> getting out of sync. Also note that <code>PreviousId</code> can be <code>null</code>, in which case no record in the database should have a <code>Next</code> pointing at that record. Is there any way to implement this? </p>