EntityType 'Breed' has no key defined. Def

2019-07-24 07:32发布

问题:

It may looks like a copy of already asked question. i have checked all the solutions but my problem is non-logical.

public class Breed
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    int id { get; set; }
    string name { get; set; }
}

Error Kamdhenu.Models.Breed: : EntityType 'Breed' has no key defined. Define the key for this EntityType. Breeds: EntityType: EntitySet 'Breeds' is based on type 'Breed' that has no keys defined.

回答1:

Define the properties as public. Without specifier they are private by default and EF won't recognize and map private properties:

public int id { get; set; }
public string name { get; set; }