Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity type has no key defined EF6

Here is my code although I already have refined the key attribute but still there is a problem.

public class Contacts
{
    [Key]
    public int ContactId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
}

The error I get is:

Entity Type 'Contacts' has no key defined. Define the key for this entity type.
Entity type: EntitySet 'Contacts' is based on type 'Contacts' that has no key defined

like image 737
Affuu Avatar asked Nov 29 '14 19:11

Affuu


1 Answers

If you are using EF Code First (not specified in your question), you will need to change the ContactId property name to ContactsId to match the convention of ClassName + Id in order to define the key for your Contacts entity type.

See the MSDN Code First Conventions: http://msdn.microsoft.com/en-us/data/jj679962.aspx

like image 99
Jason Hischier Avatar answered Nov 15 '22 06:11

Jason Hischier