title: How to add a foreign key reference to ASP.Net MVC 5 Identity?
body: <p>I have a new ASP.NET MVC 5.1 project using the ASP.Net Identity.
  It seems reliable and promise, but today i spend almost 9 hours to do a simple things if using SQL.</p>
  
  <p>My problem is, i cannot create a table via CodeFirst with foreign key reference to the default AspNetUsers table.</p>
  
  <p>For example:
  I have create a table named - <strong>Address</strong></p>
  
  <pre><code>   public class Address
      {
          [Key]
          public string UserId { get; set; }
          public string MyAddress { get; set; }
      }
  </code></pre>
  
  <p>But how can i create a foreign key reference to <strong>AspNetUsers</strong>?</p>
  
  <p>I try replace the property above by</p>
  
  <pre><code>public IdentityUser MyAddress { get; set; }
  // Or
  public virtual IdentityUser MyAddress { get; set; }
  // Or
  public virtual ApplicationUser MyAddress { get; set; }
  </code></pre>
  
  <p>But all of them still show error:</p>
  
  <pre><code>One or more validation errors were detected during model generation:
  
  MiaPos.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. 
  MiaPos.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. 
  IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. 
  IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
  </code></pre>
  
  <p>I also try override the <strong>OnModelCreating</strong> from this <a href="https://stackoverflow.com/a/20106318/83952">post</a> </p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;IdentityUserLogin&gt;().HasKey&lt;string&gt;(l =&gt; l.UserId);
      modelBuilder.Entity&lt;IdentityRole&gt;().HasKey&lt;string&gt;(r =&gt; r.Id);
      modelBuilder.Entity&lt;IdentityUserRole&gt;().HasKey(r =&gt; new { r.RoleId, r.UserId });
  }
  </code></pre>
  
  <p>But when i run <strong>Add-Migration</strong>, it create the IdentityUserLogin, IdentityRole, IdentityUserRole tables and it duplicate, just the prefix is different. (AspNet&lt;->Identity)</p>
  
  <p>Finally i do it by SQL in 10 seconds, but i just want to know why i cannot do in CodeFirst?
  And why so hard to do a such things from <strong><em>Microsoft 2014 New Technology</em></strong>.</p>
  
  <p><img src="https://i.stack.imgur.com/pddM4.png" alt="enter image description here"></p>