The entity type requires a primary key to be defined

Your working LoginItem has:

public long Id { get; set; }

Properties called *id are detected and used as the primary key by convention. You need to explicitly set the [Key] attribute otherwise.


There are multiple was to resolve this.

1.Decorate matrikelnr as [Key]

public class UserItem
{
    [Key]
    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 = "";
    }
}

2.Rename matrikelnr with matrikelnrId . With *Id , EF will consider it as PK.

 public class UserItem
{
    [Key]
    public int matrikelnrId { 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.matrikelnrId = 0;
        this.studiengang = "";
        this.user_max_klausur = "";
        this.user_semester = "";
    }
}

Entity Framework goes by convention. That means that if you have an object with a property named Id, it will assume that it is the Primary Key for the object. That's why your LoginItemclass works fine.

Your UserItem class has no such property, and therefor it can't figure out what to use as the primary key.

To fix this, affix the KeyAttribute to whatever your primary key is on your class. For example:

// Need to add the following using as well at the top of the file:
using System.ComponentModel.DataAnnotations;

public class UserItem
{
    [Key]
    public int matrikelnr { get; set; }
    public string studiengang { get; set; }
    public string user_semester { get; set; }
    public string user_max_klausur { get; set; }

    // ...
}