EntityType 'ApplicantPosition' has no key defined

后端 未结 4 764
隐瞒了意图╮
隐瞒了意图╮ 2021-01-13 05:36

When running my first asp.net mvc application I got this error I thought that entity framework automatically would create the keys of column names that end with Id? isnt it

4条回答
  •  [愿得一人]
    2021-01-13 06:05

    In your case, EF naming convention first looks for an ID (case-insensitive) column. If nothing, looks for ApplicantImageId and when it founds nothing, it raises that error.

    So, you should add the [Key] attribute on your ID:

    public class ApplicantImage
    {
        [Key]
        public int ApplicantId { get; private set; }
        public byte[] Image { get; set; }
    }
    

    and if ApplicantId column is identity in your database, you should add another attribute too:

    public class ApplicantImage
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int ApplicantId { get; private set; }
        public byte[] Image { get; set; }
    }
    

提交回复
热议问题