title: Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables?
body: <p>Here is my Model:</p>
  
  <pre><code>public class Customer
  {
      public int ID { get; set; }
  
      public int MailingAddressID { get; set; }
      public virtual Address MailingAddress { get; set; }
  
      public virtual ICollection&lt;Address&gt; Addresses { get; set; }
  }
  
  public class Address
  {
      public int ID { get; set; }
  
      public int CustomerID { get; set; }
      public virtual Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>A customer can have any number of addresses, however only one of those addresses can be a mailing address.</p>
  
  <p>I can get the One to One relationship and the One to Many working just fine if I only use one, but when I try and introduce both I get multiple CustomerID keys (CustomerID1, CustomerID2, CustomerID3) on the Addresses table. I'm really tearing my hair out over this one.</p>
  
  <p>In order to map the One to One relationship I am using the method described here <a href="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx" rel="noreferrer">http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx</a></p>