title: One or Zero to One Entity Framework Code First FluentApi
body: <ol>
  <li>I need to create fluentapi one or zero to one reference and have navigation properties on both of entities. </li>
  <li><p>EntityTwo should contain simple proerty to store foreign key (EntityOneId)</p>
  
  <pre><code>public class EntityOne
  {
      public int Id { get; set; }
      public EntityTwo EntityTwo { get; set; }
  }
  
  public class EntityTwo
  {
      public int Id { get; set; }
      public int EntityOneId { get; set; }
      public EntityOne EntityOne { get; set; }
  }
  
  public class MyDbContext : DbContext
  {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
          //some code trimmed
  
          modelBuilder.Entity&lt;EntityOne&gt;()
              .HasOptional(entity =&gt; entity.EntityTwo)
              .WithRequired();
  
          modelBuilder.Entity&lt;EntityTwo&gt;()
              .HasRequired(entity =&gt; entity.EntityOne)
              .WithMany()
              .HasForeignKey(entity =&gt; entity.EntityOneId)
              .WillCascadeOnDelete(false);
      }
  }
  </code></pre></li>
  </ol>
  
  <p>more complex scenario:</p>
  
  <pre><code>public class EntityOne
  {
      public int Id { get; set; }
  
      public EntityTwo EntityTwo { get; set; }
  }
  
  public class EntityThree
  {
      public int Id { get; set; }
  
      public EntityTwo EntityTwo { get; set; }
  }
  
  public class EntityTwo
  {
      public int Id { get; set; }
  
      public int EntityOneId { get; set; }
  
      public EntityOne EntityOne { get; set; }
  
      public int EntityThreeId { get; set; }
  
      public EntityThree EntityThree { get; set; }
  }
  
  public class MyDbContext : DbContext
  {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          //some code trimmed
  
          modelBuilder.Entity&lt;EntityOne&gt;()
              .HasOptional(entity =&gt; entity.EntityTwo)
              .WithRequired();
  
          modelBuilder.Entity&lt;EntityThree&gt;()
              .HasOptional(entity =&gt; entity.EntityTwo)
              .WithRequired();
  
          modelBuilder.Entity&lt;EntityTwo&gt;()
              .HasRequired(entity =&gt; entity.EntityOne)
              .WithMany()
              .HasForeignKey(entity =&gt; entity.EntityOneId)
              .WillCascadeOnDelete(false);
  
          modelBuilder.Entity&lt;EntityTwo&gt;()
              .HasRequired(entity =&gt; entity.EntityThree)
              .WithMany()
              .HasForeignKey(entity =&gt; entity.EntityThreeId)
              .WillCascadeOnDelete(false);
      }
  }
  </code></pre>