title: Optional one to one relationship in EF 6
body: <p>I am using Entity Framework 6 with code first.</p>
  
  <p>The ideal scenario would be the User creates a PrintType (e.g. "Business Card") and then he created multiple Layouts for this PrintType (e.g. "Center", "Left") an finally he creates a template called "Business Card with flowers". As soon he is creating this template the program should create a default-layout for THIS template.</p>
  
  <p>So there should be an optional FK that is only set when it is an default-layout for a template.</p>
  
  <p>I hope you could follow me.</p>
  
  <p>When I want to create a migration with the code below i get the following error:</p>
  
  <blockquote>
    <p>Unable to determine the principal end of an association between the
    types 'xxx.Entities.Template' and
    'xxx.Entities.Layout'. The principal end of this
    association must be explicitly configured using either the
    relationship fluent API or data annotations.</p>
  </blockquote>
  
  <pre><code>public class PrintType
  {
      public int Id { get; set; }
      public string Title { get; set; }
  
      public virtual ICollection&lt;Template&gt; Templates { get; set; }
      public virtual ICollection&lt;Layout&gt; Layouts { get; set; }
  }
  
  public class Layout
  {
      public int Id { get; set; }
      public string Title { get; set; }
  
      [Required]
      public virtual PrintType PrintType { get; set; }
      public Template Template { get; set; }
  }
  
  public class Template
  {
      public int Id { get; set; }
      public string Title { get; set; }
  
      [Required]
      public virtual PrintType PrintType { get; set; }
  
      [Required]
      public Layout Layout { get; set; }
  }
  
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Layout&gt;().HasOptional(a =&gt; a.Template).WithOptionalDependent().WillCascadeOnDelete(false);
      }
  </code></pre>