title: Entity Framework Code First Mapping Foreign Key Using Fluent API
body: <p>I have the situation where a User can have several addresses. Accordingly, I have an ICollection on my user class. But I also want the user to be able to choose a default address. So I've done the following:</p>
  
  <pre><code>public class User 
  {
      public int Id { get; set; }
      public int? DefaultAddressId { get; set; }
      [ForeignKey("DefaultAddressId")]
      public virtual Address DefaultAddress { get; set; }
      public virtual ICollection&lt;Address&gt; Addresses { get; set; }
      //properties were removed for purpose of this post
  }
  </code></pre>
  
  <p>I would like to remove the <code>public virtual Address DefaultAddress { get; set; }</code> altogether, keep the DefaultAddressId and map it using the Fluent API instead because the current setup is causing a lot of trouble (in this and other classes where I have a similar setup). So can this be done using the fluent api?</p>
  
  <p><strong>UPDATE:</strong>
  The address class currently doesn't have any reference to the User class, it's a uni-directional relationship. But yes, an address belongs to only ONE user, it's not a many to many relationship. Here's the address class:</p>
  
  <pre><code>public class Address
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Details { get; set; }
      public virtual Area Area { get; set; }
  }
  </code></pre>