I'm trying to make an optional relationship between to entities, but I'm having trouble finding the correct syntax. Actually I need a 0..1 to 0..2 relationship, but I'm guessing that once I find the way to do the 0..1 to 0..1 that will be trivial.
Simplified what I have is this:
class Foo
{
int Id { get; set; }
//navigation property
virtual Bar Bar { get; private set; }
}
class Bar
{
int Id { get; set; }
int? LeftFooId { get; set; }
[ForeignKey("LeftFooId")]
Foo LeftFoo { get; set; }
int? RightFooId{ get; set; }
[ForeignKey("RightFooId")]
Foo RightFoo { get; set; }
}
A Foo can be connected to zero or one Bar, never more. A Bar can have a LeftFoo and a RightFoo, or one or neither. The Bar property of Foo should be null if it is not referenced by a Bar, and it should contain the Bar that references it when it is referenced by a Bar.
With the code above the Bar references the Foo correctly, but EF gives the Foo table a Bar_Id and the Bar property is always null.
I've tried several different ways to setup these entity classes and I've used differenct Fluent API calls to make this work, but I don't get the wanted results.