I have an object which can optionally have a reference to a next and/or previous record. Something like this:
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;}
}
Unfortunately this does not work, instead resulting in the error message Unable to determine the principal end of an association between the types Foo and Foo
.
The idea is that by setting the PreviousId
, the Previous Foo
will get its Next
set automatically by EF. This is to prevent errors caused by Next
and Previous
getting out of sync. Also note that PreviousId
can be null
, in which case no record in the database should have a Next
pointing at that record. Is there any way to implement this?