title: InvalidOperationException: The entity type 'Enrollments' requires a primary key to be defined
body: <p>I am new to Asp.Net Core (Even to Asp.Net and web). I am using Asp.Net Core 2 with MySQL, using Pomelo.EntityFrameWorkCore.MySql (2.0.1) driver. I just created a custom dbcontext with Courses and Enrollments table, along with the default created ApplicationDbContext. The Primary Key for Enrollments is a composite key, comprising of UserId and CourseId. Below is the code :</p>
  
  <pre><code>public class CustomDbContext : DbContext
  {
      public virtual DbSet&lt;Courses&gt; Courses { get; set; }
      public virtual DbSet&lt;Enrollments&gt; Enrollments { get; set; }
  
      public CustomDbContext(DbContextOptions&lt;CustomDbContext&gt; options)
          : base(options)
      {
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Entity&lt;Courses&gt;(entity =&gt;
          {
              entity.ToTable("courses");
  
              entity.HasIndex(e =&gt; e.Name)
                  .HasName("Coursescol_UNIQUE")
                  .IsUnique();
  
              entity.Property(e =&gt; e.Id).HasColumnType("int(11)");
  
              entity.Property(e =&gt; e.Duration).HasColumnType("time");
  
              entity.Property(e =&gt; e.Name).HasMaxLength(45);
          });
  
          modelBuilder.Entity&lt;Enrollments&gt;(entity =&gt;
          {
              entity.HasKey(e =&gt; new { e.UserId, e.CourseId });
  
              entity.ToTable("enrollments");
  
              entity.HasIndex(e =&gt; e.CourseId)
                  .HasName("fk_Courses_Enrollments_CourseId_idx");
  
              entity.HasIndex(e =&gt; e.UserId)
                  .HasName("fk_Users_Enrollments_CourseId_idx");
  
              entity.HasIndex(e =&gt; new { e.UserId, e.CourseId })
                  .HasName("UniqueEnrollment")
                  .IsUnique();
  
              entity.Property(e =&gt; e.CourseId).HasColumnType("int(11)");
  
              entity.HasOne(d =&gt; d.Course)
                  .WithMany(p =&gt; p.Enrollments)
                  .HasForeignKey(d =&gt; d.CourseId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("fk_Courses_Enrollments_CourseId");
  
              entity.HasOne(d =&gt; d.User)
                  .WithMany(p =&gt; p.Enrollments)
                  .HasForeignKey(d =&gt; d.UserId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("fk_Users_Enrollments_UserId");
          });
      }
  
  }
  </code></pre>
  
  <p>The Program.cs goes like :</p>
  
  <pre><code>    public static void Main(string[] args)
      {
          var host = BuildWebHost(args);
  
          using (var scope = host.Services.CreateScope())
          {
              var services = scope.ServiceProvider;
              try
              {
                  var context = services.GetRequiredService&lt;CustomDbContext&gt;();
                  context.Database.EnsureCreated();
              }
              catch (Exception ex)
              {
                  var logger = services.GetRequiredService&lt;ILogger&lt;Program&gt;&gt;();
                  logger.LogError(ex, "An error occurred while seeding the database.");
              }
          }
  
          host.Run();
      }
  
      public static IWebHost BuildWebHost(string[] args) =&gt;
          WebHost.CreateDefaultBuilder(args)
              .UseStartup&lt;Startup&gt;()
              .Build();
  }
  </code></pre>
  
  <p>The configure services method in Startup.cs goes like :</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
              options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
  
          services.AddDbContext&lt;CustomDbContext&gt;(options =&gt;
          options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
  
          services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
              .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
              .AddDefaultTokenProviders();
  
          // Add application services.
          services.AddTransient&lt;IEmailSender, EmailSender&gt;();
  
          services.AddMvc();
      }
  </code></pre>
  
  <p>The Courses Model goes like :</p>
  
  <pre><code>public partial class Courses
  {
      public Courses()
      {
          Enrollments = new HashSet&lt;Enrollments&gt;();
      }
  
      public int Id { get; set; }
      public string Name { get; set; }
      public TimeSpan? Duration { get; set; }
  
      public ICollection&lt;Enrollments&gt; Enrollments { get; set; }
  }
  </code></pre>
  
  <p>The Enrollments Model goes like :</p>
  
  <pre><code>public partial class Enrollments
  {
      public string UserId { get; set; }
      public int CourseId { get; set; }
  
      public Courses Course { get; set; }
      public ApplicationUser User { get; set; }
  }
  </code></pre>
  
  <p>The applicationUser model goes like :</p>
  
  <pre><code>    public ApplicationUser()
      {
          Enrollments = new HashSet&lt;Enrollments&gt;();
      }
  
      public ICollection&lt;Enrollments&gt; Enrollments { get; set; }
  </code></pre>
  
  <p><strong>Now, here's what I've tried so far :</strong> </p>
  
  <ol>
  <li><p>If i add Course and Enrollment model to the ApplicationDBContext, then everything goes fine.</p></li>
  <li><p>If in CustomDBContext i have a non-composite primary Key, even then it works fine. (I just tried another example)</p></li>
  </ol>
  
  <p>Can somebody please throw some light on why is this error ? Is this the intended way to handle such a case ?</p>
  
  <p>Thanks in advance.</p>