Error: entitytype entity type has no key defined


No Primary Key is defined. Visual Studio generally looks out for an entityproperty int ID or similar and set it as a primary key. However, if our primary key is something like “Username” then we need to specify which one it is.



Include the namespace:

using System.ComponentModel.DataAnnotations;

Before your Primary Key add the following attribute: