I know that this has already been answered in one way, but I wanted to publish a different approach for those who are faced with this.
This error occurred to me, even when I had a [Key] property defined that I wanted to designate as the primary key. The error that occurred during the Update-Database command was caused by the fact that my migration name (via Add-Migration) matches the name I wanted to add. This is because the Add-Migration team created a partial class that matched the name of the object.
For instance:
public class Contacts { [Key] public int ContactId { get; set; } public string Name { get; set; } }
Doesn't work on execution:
Add-Migration Contacts Update-Database
because the signature of the migration class looked like below, and the body did not contain a property with the [Key] attribute
public sealed partial class Contacts
To solve this problem, I removed the migration with the same name and changed the Add-Migration command:
Add-Migration ContactsTable Update-Database
source share