title: EntityFramework foreign key as primary key with fluent API
body: <p>I'm trying to create a required:optional relationship between two entities, with the required entity exposing a navigation property to the optional entity, and the optional entity containing the foreign key, used as its primary key. This is what my two entities look like:</p>
  
  <pre><code>class OptionalEntity
  {
    public string RequiredEntityID { get; set; }
  }
  
  class RequiredEntity
  {
    public string ID { get; set; }
    public OptionalEntity Optional { get; set; }
  }
  </code></pre>
  
  <p>And the way I would <em>like</em> to configure them in fluent API is as follows:</p>
  
  <pre><code>// Inside OptionalEntityConfiguration class
  public OptionalEntityConfiguration()
  {
    HasKey(r =&gt; r.RequiredEntityID);
  }
  
  // Inside RequiredEntityConfiguration class
  public RequiredEntityConfiguration()
  {
    HasKey(r =&gt; r.ID);
    HasOptional(r =&gt; r.Optional)
      .WithRequired();
  
    //  How can I configure this relationship to use
    //    the RequiredEntityID property as the foreign key?
  
    HasOptional(r =&gt; r.Optional)
      .WithRequired(o =&gt; o.RequiredEntityID);
    //  This is invalid because it requires a navigation property, not an ID
  
    HasOptional(r =&gt; r.Optional)
      .WithRequired()
      .HasForeignKey(o =&gt; o.RequiredEntityID);
    //  The HasForeignKey method isn't available here
  }
  </code></pre>
  
  <p><strong>First of all is this possible, and if so what's the correct way to configure this relationship using fluent API?</strong></p>