title: EF 4.1 code-first: How to design and map these entities?
body: <p>I have 3 entities: <code>Member</code>, <code>AuthenticationToken</code>, and <code>Email</code>.</p>
  
  <ol>
  <li>Each <code>Member</code> may has <em>many</em> <code>AuthenticationTokens</code></li>
  <li>Each <code>AuthenticationToken</code> may has <em>one or zero</em> <code>Email</code></li>
  <li>Each <code>Member</code> may has <em>zero or one</em> <code>PrimaryEmail</code> (from <code>Emails</code> table). <em>Really the <code>PrimaryEmail</code> is one of the  <code>AuthenticationToken</code>s's associated  <code>Email</code></em></li>
  </ol>
  
  <p>So I have:</p>
  
  <pre><code>public class Member {
      public int MemberId { get; set; }
      public int? PrimaryEmailId { get; set; }
      public virtual Email PrimaryEmail { get; set; }
      public virtual ICollection&lt;AuthenticationToken&gt; AuthenticationTokens { get; set; }
  }
  
  public class AuthenticationToken {
      public int AuthenticationTokenId { get; set; }
      public int MemberId { get; set; }
      public virtual Member Member { get; set; }
      public virtual Email Email { get; set; }
  }
  
  public class Email {
      public int EmailId { get; set; } // is same as AuthenticationTokenId that the email associated with it
  }
  </code></pre>
  
  <p>With design I explained above, I can add Member and AuthenticationToken, but when I want to attach a Email to a Member or AuthenticationToken (or both) I give this error:</p>
  
  <blockquote>
    <p>The INSERT statement conflicted with the FOREIGN KEY constraint etc.</p>
  </blockquote>
  
  <p>Is this design correct??? 
  How can I design my tables (and entities) to achieve my purpose? 
  And how can I map my entities in code-first? Have you any idea please?</p>
  
  <p><img src="https://i.stack.imgur.com/bAdpj.gif" alt="enter image description here"></p>