title: Entity Framework Parent Child - Child refers to parent more than once
body: <p>I have a situation with EF5 and a complex object. The basics is that I have a parent to child complex object, but the child refers back to the parent, more than once. I have tried various options but am not finding a solution that answers the question. The closest I have got is <a href="https://stackoverflow.com/questions/9241165/entity-framework-code-first-class-with-parent-and-children-of-same-type-as-its">this answer</a> (option 2c)</p>
  
  <p>My model looks like below:</p>
  
  <pre><code>public class StaffMember
  {
     public virtual Guid StafId { get; set; }
     // other props
  
     // List of leave apps (Approved/Cancelled etc)
     public virtual ICollection&lt;StaffLeaveApp&gt; LeaveApps { get; set; }
  }
  
  //Staff Leave Application
  public class StaffLeaveApp
  {
     public virtual Guid LeaveId { get; set; }
     public virtual Guid StaffId { get; set; }
     // other props...
  
     // Leave approved by? (2 approvals required)
     public virtual StaffMember ApprovedBy1 { get; set; }
     public virtual StaffMember ApprovedBy2 { get; set; }
  }
  </code></pre>
  
  <p>my mappings look like this</p>
  
  <pre><code>public class StaffMap : EntityTypeConfiguration&lt;StaffMember&gt;
  {
      public StaffMap()
      {
          ToTable("tblStaffMembers");
          HasKey(x =&gt; x.StaffId);
          // other mappings...
  
          HasMany(x =&gt; x.LeaveApps);
      }
  }
  
  public class StaffLeaveAppMap: EntityTypeConfiguration&lt;StaffLeaveApp&gt;
  {
      public StaffLeaveAppMap()
      {
          ToTable("tblStaffMembersLeaveApps");
          HasKey(x =&gt; x.LeaveId);
          Property(x =&gt; x.StaffId).HasColumnName("StaffID");
  
          //Child Relationships
          HasOptional(x =&gt; x.ApprovedBy1).WithMany().Map(m =&gt; m.MapKey("LeaveApprovedBy1"));
          HasOptional(x =&gt; x.ApprovedBy2).WithMany().Map(m =&gt; m.MapKey("LeaveApprovedBy2"));
      }
  }
  </code></pre>
  
  <p>Table (sorry, no images)</p>
  
  <pre><code>  StaffID uniqueidentifier (FK - tblStaffMembers)
    LeaveID uniqueidentifier (PK)
    LeaveApprovedBy1 uniqueidentifier (FK - tblStaffMembers)
    LeaveApprovedBy2 uniqueidentifier (FK - tblStaffMembers)
  </code></pre>
  
  <p>The business rule says: a staff member has "many" leave applications and a leave application belongs to a single staff member. Each application requires the approval of 2 staff members (managers) before it is "approved".</p>
  
  <p>How would I map the above using EF so that a single staff member has a "many" leave applications (working already) and a leave application is mapped back to a staff member whom approved it for the first approval and then again for the seond approval. If I use the one mapping for "ApprovedBy1" only then EF is happy and all works as expected. The moment I add the second approval mapping EF struggles with the SQL queries it generates.</p>
  
  <p>I am not sure how to tell EF to map back to the StaffMembers table to specify whom approved the application at level 1 and whom approved it at level 2. It almost ends up being a many to many relationship.</p>
  
  <p>Any ideas?</p>