title: Entity Framework table with multiple optional one to one relationships
body: <p>There are a few questions out there but none of them have been much help to me so I decided to ask my own.  </p>
  
  <p>I want to use Entity Framework code first to create a strange relationship. I have three entities (<code>Qualification</code>, <code>Test</code> and <code>Procedure</code>) that all have the option of having a document(s). I am attempting to store all the documents in one table. How do I configure the relationship between all these entities.</p>
  
  <ul>
  <li><code>Qualification</code> has one to one optional <code>Document</code></li>
  <li><code>Test</code> has one to one optional <code>Document</code></li>
  <li><code>Procedure</code> has one to many optional <code>Documents</code></li>
  </ul>
  
  <p><code>Document</code> has a required relationship to one <code>Qualification</code>, <code>Test</code> or <code>Procedure</code>.</p>
  
  <pre><code>public class Assessment
  {
      public int AssessmentId { get; set; }
      //Other properties in Assessment
      public Document Document { get; set; }
  }
  
  public class Qualification
  {
      public int QualificationId{ get; set; }
      //Other properties in Qualification
      public Document Document { get; set; }
  }
  
  public class Procedure
  {
      public int ProcedureId { get; set; }
      //Other properties in Procedure
      public ICollection&lt;Document&gt; Documents { get; set; }
  }
  
  public class Document
  {
      public int DocumentId { get; set; }
      //Other properties in Document
      public Qualification Qualification { get; set; }
      public Assessment Assessment { get; set; }
      public Procedure Procedure { get; set; }
  }
  
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      // Not sure what goes here, but this was not working 
      modelBuilder.Entity&lt;Document&gt;()
          .HasOptional(d =&gt; d.Qualification).WithOptionalPrincipal(q =&gt; q.Document);
  
      modelBuilder.Entity&lt;Document&gt;()
          .HasOptional(d =&gt; d.Asssessment).WithOptionalPrincipal(q =&gt; q.Document);
  }
  </code></pre>
  
  <p>When adding a migration I am getting an error:</p>
  
  <blockquote>
    <p>Unable to determine the principal end of an association between the types. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.</p>
  </blockquote>
  
  <p>I am not sure how to do this. I had a nullable int <code>DocumentId</code> in <code>Assessment</code> and <code>Qualification</code> but that will not work b/c <code>Procedure</code> can hold a collection so <code>Document</code> must hold the Id. Does <code>Document</code> hold a nullable Id for each possible parent? Help please.</p>