title: How do I add a foreign key to Identity user in EF Core?
body: <p>Let's say I have a <code>Todo</code> model with a corresponding <code>Todo</code> table (<code>DbSet&lt;Todo&gt;</code>) in an EF-generated database. The table will store a todo item per row. Each user (in an ASP.NET Core Identity + IdentityServer app) will be associated with multiple todo items (one-to-many, a user can have multiple todo items).</p>
  
  <p>The way I'd do it is to add a <code>UserId</code> foreign key to the <code>Todo</code> model that represents the user that owns the todo item .</p>
  
  <p>How would I do that with EF Core while utilizing the classes proved by ASP.NET Core Identity? My intuition was to add the following to the <code>Todo</code> class model:</p>
  
  <pre><code>public string UserId { get; set; }
  [ForeignKey("UserId")]
  public ApplicationUser User { get; set; }
  </code></pre>
  
  <p>This uses <code>ApplicationUser</code> (the ASP.NET Core Identity default user class model) as discovery to populate <code>UserId</code> with a foreign key.</p>
  
  <p>Here's the problem: when I run <code>Update-Database</code> I get an error! It says <code>The entity type 'IdentityUserLogin&lt;string&gt;' requires a primary key to be defined.</code>. </p>
  
  <p>Browsing through the code for <code>IdentityUserLogin</code>, I can see it has a property <code>UserId</code> which should be considered a primary key, right? </p>
  
  <p>Anyway, to make sure its registered as a primary key, I added this code to <code>ApplicationUser</code>'s <code>OnModelCreating</code> method: <code>builder.Entity&lt;ApplicationUser&gt;().HasKey(u =&gt; u.Id);</code>. I still get the same error! Not sure what I'm doing wrong here :(. Edit: Yes, I called the base class so that's not the problem.</p>
  
  <p>Note: I know I can add a field to each user that carries a collection of todo item Ids instead of having a foreign key, but I'm not sure if that's the good way of doing this. Guidance on this issue appreciated.</p>