title: EF 4.1 Code First ModelBuilder HasForeignKey for One to One Relationships
body: <p>Very simply I am using Entity Framework 4.1 code first and I would like to replace my [ForeignKey(..)] attributes with fluent calls on modelBuilder instead. Something similar to WithRequired(..) and HasForeignKey(..) below which tie an explicit foreign key property (CreatedBySessionId) together with the associated navigation property (CreatedBySession). But I would like to do this for a one to one relationsip instead of a one to many:</p>
  
  <pre><code>modelBuilder.Entity&lt;..&gt;().HasMany(..).WithRequired(x =&gt; x.CreatedBySession).HasForeignKey(x =&gt; x.CreatedBySessionId)
  </code></pre>
  
  <p>A more concrete example is below. This works quite happily with the [ForeignKey(..)] attribute but I'd like to do away with it and configure it purely on modelbuilder.</p>
  
  <pre><code>public class VendorApplication
  {
      public int VendorApplicationId { get; set; }
  
      public int CreatedBySessionId { get; set; }
      public virtual Session CreatedBySession { get; set; }
  }
  
  public class Session
  {
      public int SessionId { get; set; }
  
      [ForeignKey("CurrentApplication")]
      public int? CurrentApplicationId { get; set; }
      public virtual VendorApplication CurrentApplication { get; set; }
  
      public virtual ICollection&lt;VendorApplication&gt; Applications { get; set; }
  }
  
  public class MyDataContext: DbContext
  {
      public IDbSet&lt;VendorApplication&gt; Applications { get; set; }
      public IDbSet&lt;Session&gt; Sessions { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Session&gt;().HasMany(x =&gt; x.Applications).WithRequired(x =&gt; x.CreatedBySession).HasForeignKey(x =&gt; x.CreatedBySessionId).WillCascadeOnDelete(false); 
          // Note: We have to  turn off Cascade delete on Session &lt;-&gt; VendorApplication relationship so that SQL doesn't complain about cyclic cascading deletes
      }
  }
  </code></pre>
  
  <p>Here a Session can be responsible for creating many VendorApplications (Session.Applications), but a Session is working on at most one VendorApplication at a time (Session.CurrentApplication). I would like to tie the CurrentApplicationId property with the CurrentApplication navigation property in modelBuilder instead of via the [ForeignKey(..)] attribute.</p>
  
  <p><strong>Things I've Tried</strong> </p>
  
  <p>When you remove the [ForeignKey(..)] attribute the CurrentApplication property generates a CurrentApplication_VendorApplicationId column in the database which is not tied to the CurrentApplicationId column.</p>
  
  <p>I've tried explicitly mapping the relationship using the CurrentApplicationId column name as below, but obviously this generates an error because the database column name "CurrentApplicationId" is already being used by the property Session.CurrentApplicationId:</p>
  
  <pre><code>modelBuilder.Entity&lt;Session&gt;().HasOptional(x =&gt; x.CurrentApplication).WithOptionalDependent().Map(config =&gt; config.MapKey("CurrentApplicationId"));
  </code></pre>
  
  <p>It feels like I'm missing something very obvious here since all I want to do is perform the same operation that [ForeignKey(..)] does but within the model builder. Or is it a case that this is bad practise and was explicitly left out?</p>