title: EF6 Code First - Configure One-to-One relationship with unmatched foreign keys
body: <p>I'm using Code First to define the schema of an existing database.  The problem I'm having is that they key names in the tables don't quite align.  This works fine with one-to-many relationships because I can use the <code>HasForeignKey()</code> method, but there does't seem to be an equivalent for one-to-one relationships.  My table definitions are:</p>
  
  <pre><code>namespace Data.Mappings {
  
      internal class DocumentTypeConfiguration : EntityTypeConfiguration&lt;Document&gt; {
  
          public DocumentTypeConfiguration() {
              ToTable("ProsDocs");
  
              HasKey(m =&gt; m.Id);
  
              Property(m =&gt; m.Id)
                  .HasColumnName("ProsDocId")
                  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
              // ---- This is the foreign key ----
              Property(m =&gt; m.TypeId)
                  .HasColumnName("ProsDocTypeId")
                  .HasMaxLength(3);
  
              HasRequired(d =&gt; d.DocType)
                  .WithRequiredDependent(dt =&gt; dt.Document);
                  // I need to specify here that the foreign key is named "DocTypeId" and not "DocType_Id"
          }
      }
  
      internal class DocTypeTypeConfiguration : EntityTypeConfiguration&lt;DocType&gt; {
  
          public DocTypeTypeConfiguration() {
              ToTable("DocType");
  
              HasKey(m =&gt; m.Id);
  
              // ---- This is the "child" end of the foreign key ----
              Property(m =&gt; m.Id)
                  .HasColumnName("DocTypeId")
                  .HasMaxLength(4);
  
              Property(m =&gt; m.FullName)
                  .HasColumnName("DocTypeDesc")
                  .HasMaxLength(255);
  
              Property(m =&gt; m.Priority)
                  .HasColumnName("DocPriority");
  
              // Or would it be easier to define it here?
  //            HasRequired(m =&gt; m.Document)
  //                .WithRequiredDependent(d =&gt; d.DocType);
          }
      }
  }
  </code></pre>
  
  <p>To clarify the model, each <code>Document</code> has a <code>DocType</code>, with the foreign key relationship of <code>Document.ProsDocTypeId --&gt; DocType.Id</code>.</p>
  
  <p>Is there a way to define what the column names are for keys for one-to-one relationships?</p>
  
  <p><strong>EDIT:</strong>  I guess my schema isn't clear.</p>
  
  <pre><code>dbo.DocType
  -----------
  DocTypeId char(4) (PK)
  DocTypeDesc varchar(255)
  
  dbo.ProsDocs
  ------------
  ProsDocId int (PK)
  ProsDocTypeId char(4)
  </code></pre>
  
  <p>Basically, I need the resulting query to look like this:</p>
  
  <pre><code>SELECT 
      [Extent1].[ProsDocId] AS [ProsDocId], 
      [Extent2].[DocTypeId] AS [DocTypeId]
      FROM  [dbo].[ProsDocs] AS [Extent1]
      LEFT OUTER JOIN [dbo].[DocType] AS [Extent2] ON [Extent1].[ProsDocTypeId] = [Extent2].[DocTypeId]
      WHERE [Extent1].[ProsId] = @EntityKeyValue1
  </code></pre>
  
  <p>But instead, because EF assumes I want to use the primary key (<code>dbo.ProsDocs.ProsDocId</code>) instead of a foreign key (<code>dbo.ProsDocs.DocTypeId</code>), the query it generates is this: </p>
  
  <pre><code>SELECT 
      [Extent1].[ProsDocId] AS [ProsDocId], 
      [Extent2].[DocTypeId] AS [DocTypeId]
      FROM  [dbo].[ProsDocs] AS [Extent1]
      LEFT OUTER JOIN [dbo].[DocType] AS [Extent2] ON [Extent1].[ProsDocId] = [Extent2].[ProsDocTypeId]
      WHERE [Extent1].[ProsId] = @EntityKeyValue1
  </code></pre>
  
  <p>The difference lies here:</p>
  
  <p>Ideal query:</p>
  
  <pre><code>LEFT OUTER JOIN [dbo].[DocType] AS [Extent2] ON [Extent1].[ProsDocTypeId] = [Extent2].[DocTypeId]
  </code></pre>
  
  <p>Current query:</p>
  
  <pre><code>LEFT OUTER JOIN [dbo].[DocType] AS [Extent2] ON [Extent1].[ProsDocId] = [Extent2].[ProsDocTypeId]
  </code></pre>
  
  <p>I need to create a <em>one-to-one relationship</em> between <code>dbo.ProsDocs.ProsDocTypeId</code> and <code>dbo.DocType.DocTypeId</code>.  The problem EF is having with it is that it wants to create the relationship using only the primary keys, not foreign keys.  How do I specify the column name of the foreign key such that <em>each <code>Document</code> has exactly one <code>DocType</code>?</em></p>