title: Entity Framework Code First One-to-One Required-Required Relationship
body: <p>When using Entity Framework Code First 4.3.1 it is possible to create relationships with a multiplicity of 1-to-1. That is, one entity on each end of the relationship.</p>
  
  <p>It is possible to configure 1-to-1 relationships to be <strong>required-required</strong> or <strong>required-optional</strong> ^. However, when I switch between the two I do not see any differences in:</p>
  
  <ul>
  <li>The database schema generated. I am targeting SQL Server 2008.</li>
  <li>The runtime behaviour of EF.</li>
  </ul>
  
  <p>As such, I am able to create a <strong>RequiredPrincipalAs</strong> record without a corresponding <strong>RequiredDependentAs</strong> record, despite the relationship being configured as <strong>required-required</strong>. This seems to contradict the documentation for <strong>HasRequired(...)</strong>:</p>
  
  <blockquote>
    <p>Configures a required relationship from this entity type. Instances of the entity type will not be able to be saved to the database unless this relationship is specified. The foreign key in the database will be non-nullable.</p>
    
    <p><a href="http://msdn.microsoft.com/en-us/library/gg671317">http://msdn.microsoft.com/en-us/library/gg671317</a></p>
  </blockquote>
  
  <p>The <strong>required-required</strong> relationship entities:</p>
  
  <pre><code>public class RequiredPrincipalA
  {
      public int Id { get; set; }
      public virtual RequiredDependentA DependentA { get; set; }
  }
  
  public class RequiredDependentA
  {
      public int Id { get; set; }
      public virtual RequiredPrincipalA PrincipalA { get; set; }
  }
  </code></pre>
  
  <p>The <strong>required-optional</strong> relationship entities:</p>
  
  <pre><code>public class RequiredPrincipalB
  {
      public int Id { get; set; }
      public virtual OptionalDependentB DependentB { get; set; }
  }
  
  public class OptionalDependentB
  {
      public int Id { get; set; }
      public virtual RequiredPrincipalB PrincipalB { get; set; }
  }
  </code></pre>
  
  <p>The DbContext and model configuration:</p>
  
  <pre><code>public class AppContext : DbContext
  {
      public DbSet&lt;RequiredPrincipalA&gt; PrincipalAs { get; set; }
      public DbSet&lt;RequiredDependentA&gt; DependentAs { get; set; }
  
      public DbSet&lt;RequiredPrincipalB&gt; PrincipalBs { get; set; }
      public DbSet&lt;OptionalDependentB&gt; DependentBs { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;RequiredPrincipalA&gt;()
              .HasRequired(o =&gt; o.DependentA)
              .WithRequiredPrincipal(o =&gt; o.PrincipalA);
  
          modelBuilder.Entity&lt;RequiredPrincipalB&gt;()
              .HasOptional(o =&gt; o.DependentB)
              .WithRequired(o =&gt; o.PrincipalB);
      }
  }
  </code></pre>
  
  <p>The test code:</p>
  
  <pre><code>Database.SetInitializer(new DropCreateDatabaseAlways&lt;AppContext&gt;());
  
  using (var ctx = new AppContext())
  {
      ctx.Database.Initialize(force: false);
  
      ctx.PrincipalAs.Add(new RequiredPrincipalA());
      ctx.PrincipalBs.Add(new RequiredPrincipalB());
  
      ctx.SaveChanges();
  }
  </code></pre>
  
  <p>I am aware I could add a <strong>[Required]</strong> data attribute to the navigation properties of <strong>RequiredPrincipalA.DependentA</strong> and <strong>RequiredDependentA.PrincipalA</strong>. This would cause EF validation to prevent the scenario above. However, I do not want to do this because it also validates the navigation property is populated when updating an existing entity. This means the application has to pre-fetch the entity at the other end of the relationship for every update.</p>
  
  <p>Why do I not see any difference in the behaviour of EF just when changing a relationship between <strong>required-required</strong> and <strong>required-optional</strong>?</p>
  
  <p>^ <em>Note that optional-optional is also supported but this doesn't form part of my question. There are obvious differences in the generated database schema and runtime behaviour when an optional-optional relationship is configured.</em></p>