title: Entity Framework: one to zero or one relationship with foreign key on principal
body: <p>I have a 1:0..1 relationship that I'd like to map with EF 6 using fluent API. The relation consists of a principal, which may or may not have a dependent. A dependent must always have a principal.</p>
  
  <p>In the principal, I need to have access to the Id of the dependent.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>public class Principal
  {
      public int Id {get; private set; }
  
      public int? DependentId { get; private set; }
      public virtual Dependent Dependent { get; private set; }
  }
  
  public class Dependent
  {
      public int Id { get; private set; }
  
      public virtual Principal Principal { get; private set; }
  }
  </code></pre>
  
  <p>My mapping looks like this:</p>
  
  <pre><code>    public class PrincipalMap : EntityTypeConfiguration&lt;Principal&gt;
      {
          public PrincipalMap()
          {
              ToTable("PRINCIPALS");
  
              HasKey(x =&gt; x.Id);
  
              Property(x =&gt; x.Id)
                  .HasColumnName("PRINCIPALID")
                  .IsRequired()
                  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
              Property(x =&gt; x.DependentId)
                  .HasColumnName("DEPENDENTID")
                  .IsOptional();
          }
      }
  
      public class DependentMap : EntityTypeConfiguration&lt;Dependent&gt;
      {
          public DependentMap()
          {
              ToTable("DEPENDENTS");
  
              HasKey(x =&gt; x.Id);
  
              Property(x =&gt; x.Id)
                  .HasColumnName("DEPENDENTID")
                  .IsRequired()
                  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
              HasRequired(x =&gt; x.Principal).WithOptional(x =&gt; x.Dependent).Map(x =&gt; x.MapKey("PRINCIPALID")).WillCascadeOnDelete();
          }
      }
  </code></pre>
  
  <p>Which results in the following migration:</p>
  
  <pre><code>            CreateTable(
                  "dbo.PRINCIPALS",
                  c =&gt; new
                      {
                          PRINCIPALID = c.Int(nullable: false, identity: true),
                          DEPENDENTID = c.Int(),
                      })
                  .PrimaryKey(t =&gt; t.PRINCIPALID);
  
              CreateTable(
                  "dbo.DEPENDENTS",
                  c =&gt; new
                      {
                          DEPENDENTID = c.Int(nullable: false, identity: true),
                          PRINCIPALID = c.Int(nullable: false),
                      })
                  .PrimaryKey(t =&gt; t.DEPENDENTID)
                  .ForeignKey("dbo.PRINCIPALS", t =&gt; t.PRINCIPALID, cascadeDelete: true)
                  .Index(t =&gt; t.PRINCIPALID);
  </code></pre>
  
  <p>As you can see, the column <code>DEPENDENTID</code> is not a foreign key. When running the program and associating a dependent object to a principal, <strong>the <code>DependentId</code> property remains empty</strong>, i.e. EF does not recognize it to be related to the dependent itself.</p>
  
  <p>What am I doing wrong?</p>