title: The entity type 'UserGroupLink' requires a primary key to be defined. This is a many to many relationship
body: <p>I'm getting <em>The entity type 'UserGroupLink' requires a primary key to be defined.</em> I got this error when I run <em>Add-Migration</em> in Package Manager Console. I tried various stackoverflow answers, none of them is working for me. <strong>User</strong> may be part of any number of <strong>UserGroups</strong> and <strong>UserGroup</strong> can have one or more <strong>User</strong>. <strong>UserGroupLink</strong> is the table bridges this relationship as below Fluent Api configuration. Please help me in this. I followed this article <a href="https://github.com/aspnet/EntityFramework.Docs/blob/live/entity-framework/core/modeling/keys.md#fluent-api" rel="nofollow noreferrer">https://github.com/aspnet/EntityFramework.Docs/blob/live/entity-framework/core/modeling/keys.md#fluent-api</a>.</p>
  
  <pre><code>modelBuilder.Entity&lt;UserGroup&gt;().Property(u =&gt; u.UserGroupId).ValueGeneratedOnAdd();
          modelBuilder.Entity&lt;UserGroup&gt;().Property(u =&gt; u.Name).IsRequired().HasMaxLength(50);
          modelBuilder.Entity&lt;UserGroup&gt;().HasOne(u =&gt; u.Project).WithMany(p =&gt; p.UserGroups).HasForeignKey(f =&gt; f.ProjectId);
  
          modelBuilder.Entity&lt;User&gt;().HasKey(u =&gt; u.Email);
          modelBuilder.Entity&lt;User&gt;().Property(u =&gt; u.Name).IsRequired().HasMaxLength(50);
          modelBuilder.Entity&lt;User&gt;().Property(u =&gt; u.Email).IsRequired().HasMaxLength(50);
          modelBuilder.Entity&lt;User&gt;().Property(u =&gt; u.Role).IsRequired().HasMaxLength(50);
  
  modelBuilder.Entity&lt;UserGroupLink&gt;().HasKey(l =&gt; new { l.UserGroupId, l.Email }); //Key is already defined here. Still error.
          modelBuilder.Entity&lt;UserGroupLink&gt;().HasOne(l =&gt; l.UserGroup).WithMany(u =&gt; u.UserGroupLinks).HasForeignKey(l =&gt; l.UserGroupId);
          modelBuilder.Entity&lt;UserGroupLink&gt;().HasOne(l =&gt; l.User).WithMany(u =&gt; u.UserGroupLinks).HasForeignKey(l =&gt; l.Email);
  </code></pre>
  
  <p>Class</p>
  
  <pre><code>public class UserGroupLink
  {
      public string Email { get; set; }
      public User User { get; set; }
      public int UserGroupId { get; set; }        
      public UserGroup UserGroup { get; set; }
      public UserGroupLink()
      {
  
      }
      public UserGroupLink(User user, UserGroup userGroup)
      {
          User = user;
          UserGroup = userGroup;
      }
  }
  public class UserGroup
  {
      public int UserGroupId { get; set; }
      public string Name { get; set; }
      public int ProjectId { get; protected set; }
      public Project Project { get; protected set; }
      public IList&lt;UserGroupLink&gt; UserGroupLinks { get; set; }
  }
  public class User
  {
      public string Name { get; set; }
      public string Email { get; set; }
      public string Role { get; set; }
      public IList&lt;UserGroupLink&gt; UserGroupLinks { get; set; }
  }
  </code></pre>