title: WithOptionalDependent vs WithOptionalPrinciple - Definitive Answer?
body: <p>I thought it might be helpful to get a definitive answer on when to use <a href="http://msdn.microsoft.com/en-us/library/gg696744%28v=vs.113%29.aspx"><code>WithOptionalDependent</code></a> and when to use <a href="http://msdn.microsoft.com/en-us/library/gg679107%28v=vs.113%29.aspx"><code>WithOptionalPrincipal</code></a>. The help for the two functions is a little unclear, and I find myself digging through multiple Stack Overflow answers and answers on other sites combining answers in order to feel confident I've got the relationship going the correct direction.  </p>
  
  <p>Here's what MSDN says about <a href="http://msdn.microsoft.com/en-us/library/gg696744%28v=vs.113%29.aspx"><code>WithOptionalDependent</code></a>:</p>
  
  <blockquote>
    <p>Configures the relationship to be optional:optional without a
    navigation property on the other side of the relationship. The entity
    type being configured will be the dependent and contain a foreign key
    to the principal. The entity type that the relationship targets will
    be the principal in the relationship.</p>
  </blockquote>
  
  <p>and here is what it says about <a href="http://msdn.microsoft.com/en-us/library/gg679107%28v=vs.113%29.aspx"><code>WithOptionalPrincipal</code></a>:</p>
  
  <blockquote>
    <p>Configures the relationship to be optional:optional without a
    navigation property on the other side of the relationship. The entity
    type being configured will be the principal in the relationship. The
    entity type that the relationship targets will be the dependent and
    contain a foreign key to the principal.</p>
  </blockquote>
  
  <p>The line "The entity type being configured" is the part that always confuses me (and I assume others).</p>
  
  <p>In this example:</p>
  
  <pre><code>class MyEntityA
  {
      [Key]
      public int Id { get; set; }
      public int BId { get; set; }
      [ForeignKey("BId")]
      public MyEntityB B { get; set; }
  }
  
  class MyEntityB
  {
      [Key]
      public int Id { get; set; }
  }
  
  modelBuilder.Entity&lt;MyEntityA&gt;().HasOptional(a =&gt; a.B).WithOptionalDependent();
  </code></pre>
  
  <p>is "The entity type being configured" referring to <code>MyEntityA</code> or <code>MyEntityB</code>? I assume it is the former. </p>
  
  <p>If that's correct, what's an example of when you'd use <code>WithOptionalPrincipal</code>?</p>
  
  <p>I actually think in my code example it should really be <code>WithMany</code> and neither of the WithOptional options. Clearly I'm still confused!</p>
  
  <p>There are overloads for both of these functions that take the navigation property going the other direction. I assume those overloads don't change those answers, but please correct me if I'm wrong.</p>
  
  <p>I hope this will be helpful to the larger community as well.</p>