Set Up and Seed Code First One to Zero or One Relationship
===

Perhaps I'm approaching this the wrong way, but I have a complex scenario I'm trying to set up in EF Code First. I have a FilterSegment class which holds information about segments of an audience that will receive campaign deliverables. Each segment has a condition associated with it, but conditions can be complex so I have set them up in a recursive FilterConditionOperand table. A FilterSegment is required to have a root FilterConditionOperand, while FilterConditionOperands may have child operands enabling complex conditions. So each segment has a required root condition, while conditions have an optional associated segment and an optional parent condition. **A sample of how I would like to be able to seed a segment with the condition** `(test1 = 3) AND (test2 > 4)` might help to understand the structure:

    new List<FilterSegment>{
        /*1*/ new FilterSegment{ Name="Test Segment", RootConditionID=1}
    }.ForEach(fs => context.FilterSegments.Add(fs));

    new List<FilterConditionOperand>{
        /*1*/ new FilterConditionOperand{ FilterSegmentID=1, OperandType= "operator", ResolvesTo = "bool", Value="AND"},
        /*2*/ new FilterConditionOperand{ OperandType = "operator", ParentID=1, ResolvesTo="bool", Value="="},
        /*3*/ new FilterConditionOperand{ OperandType = "variable", ParentID=2, ResolvesTo= "int", Value="test1"},
        /*4*/ new FilterConditionOperand{ OperandType = "literal", ParentID=2, ResolvesTo= "int", Value="3"},
        /*5*/ new FilterConditionOperand{ OperandType = "operator", ParentID=1, ResolvesTo="bool", Value=">"},
        /*6*/ new FilterConditionOperand{ OperandType = "variable", ParentID=5, ResolvesTo="int", Value="test2"},
        /*7*/ new FilterConditionOperand{ OperandType = "literal", ParentID=5, ResolvesTo="int", Value="4"}
    }.ForEach(fco => context.FilterSegments.Add(fco));
The associated models would look something like this (simplified):

    public class FilterSegment
    {
        public int ID { get; set; }
        public string Name { get; set; }
    
        public int RootConditionID { get; set; }
        public virtual FilterConditionOperand RootCondition { get; set; }
    }
    public class FilterConditionOperand
    {
        public int ID { get; set; }
        public int? FilterSegmentID { get; set; } //only has a value if this is the root for the segment
        public int? ParentID { get; set; } //only has a value if this is not the root condition of a segment
        public string OperandType { get; set; }
        public string ResolvesTo { get; set; }
        public string Value { get; set; }
    
        public virtual FilterSegment FilterSegment { get; set; }
        public virtual FilterConditionOperand Parent { get; set; }
        public virtual ICollection<FilterConditionOperand> Children { get; set; }
    }
I set up the relationships in my DbContext OnModelCreating method as follows:

    modelBuilder.Entity<FilterSegment>()
        .HasRequired<FilterConditionOperand>(fs => fs.RootCondition)
        .WithOptional(fco => fco.FilterSegment);
    modelBuilder.Entity<FilterConditionOperand>()
        .HasOptional<FilterConditionOperand>(child => child.Parent)
        .WithMany(parent => parent.Children);
When I run my project, 1) my RootConditionID and FilterSegmentID fields are not set up as foreign keys in their tables (I am almost sure this is because I haven't mapped them correctly, but I would like to know the best way to specify the mapping), and 2) I get an error related to seeding the database: "Unable to determine the principal end of the 'wdlive.Models.FilterSegment_RootCondition' relationship. Multiple added entities may have the same primary key." I believe this is because the seed function is trying to assign a root operand to the segment before it has been created, but I feel certain there should be a proper way to do this sort of relational seeding.

Questions:

 1. What is the best way to set up the foreign key relationships for my FilterSegment.RootConditionID and FilterConditionOperands.FilterSegmentID fields (preferably using Fluent)?
 2. How can I seed a one to zero or one relationship specifying ids of related entities?

Thanks in advance for any help you may be able to provide.