title: The entity type requires a primary key to be defined
body: <p>I'm writing an ASP.NET Web API right now and everything works just fine for 2 controllers. Now I try to do exactly the same as before but this time I get a weird error: </p>
  
  <blockquote>
    <p>System.InvalidOperationException: "The entity type 'UserItem' requires a primary key to be defined."</p>
  </blockquote>
  
  <p>Well then, why does <code>UserItem</code> needs a primary key when the others don't?</p>
  
  <p>This is my <code>UserItem</code> class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace ModulApi.Models
  {
      public class UserItem
      {
          public int matrikelnr { get; set; }
          public string studiengang { get; set; }
          public string user_semester { get; set; }
          public string user_max_klausur { get; set; }
  
          //Not necessary Constructor. I try to fix the primary Key error.
          public UserItem()
          {
              this.matrikelnr = 0;
              this.studiengang = "";
              this.user_max_klausur = "";
              this.user_semester = "";
          }
      }
  }
  </code></pre>
  
  <p>And my WORKING <code>LoginItem</code> class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace ModulApi.Models
  {
      public class LoginItem
      {
          public long Id { get; set; }
          public string username { get; set; }
          public string password { get; set; }
          public string matrikelnr { get; set; }
          public string email { get; set; }
          public string email_verified { get; set; }
  
          public LoginItem()
          {
              this.Id = 0;
              this.username = "";
              this.password = "";
              this.matrikelnr = "";
              this.email = "";
              this.email_verified = "";
           }
      }
  }
  </code></pre>
  
  <p>As you see, I got getter and setter set up, so the error can't be there.</p>
  
  <p>Here is where the error occurs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using ModulApi.Models;
  using ModulApi.DBConnectors;
  
  // For more information on enabling Web API for empty projects, visit 
  https://go.microsoft.com/fwlink/?LinkID=397860
  
  namespace ModulApi.Controllers
  {
      [Route("api/user")]
      public class UserController : Controller
      {
          private readonly UserContext _context;
  
          public UserController(UserContext context)
          {
              _context = context;
  
              if (_context.UserItems.Count() == 0)  &lt;--- Error right here
              {
                   getDataFromConnector(_context);
              }
          }
  
          private void getDataFromConnector(UserContext context)
          {
              //Getting my Data from Database method
          }
          .
          .
  </code></pre>
  
  <p>Well Since it is in a Context call, I'll attach UserContext as well, but again it is the same as in LoginContext, which works just fine.</p>
  
  <p>UserContext: </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.EntityFrameworkCore;
  
  namespace ModulApi.Models
  {
      public class UserContext : DbContext
      {
          public UserContext(DbContextOptions&lt;UserContext&gt; options) : base(options)
          {
          }
  
          public DbSet&lt;UserItem&gt; UserItems { get; set; }
      }
  }
  </code></pre>
  
  <p>Has anyone a clue why I get this weird error? And why all other Controllers work fine which do exactly the same?</p>