EntityType'ApplyantPosition'没有定义键

Lui*_*cia 5 asp.net-mvc entity-framework entity-framework-4 entity-framework-4.1 asp.net-mvc-3

运行我的第一个asp.net mvc应用程序时遇到此错误我认为实体框架会自动创建以Id结尾的列名的键吗?不正确吗?

如您所见,ApplicantPositionID将是一个包含2列作为主键的表,因为它与申请人和职位相关.

在模型生成期间检测到一个或多个验证错误:

System.Data.Edm.EdmEntityType: : EntityType 'ApplicantImage' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'ApplicationPositionHistory' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet ?ApplicantsPositions? is based on type ?ApplicantPosition? that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet ?ApplicantImages? is based on type ?ApplicantImage? that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet ?ApplicationsPositionHistory? is based on type ?ApplicationPositionHistory? that has no keys defined.
Run Code Online (Sandbox Code Playgroud)

此行中出现错误:

public ActionResult Index()
        {
            return View(db.Positions.ToList());
        }
Run Code Online (Sandbox Code Playgroud)

我的模型如下:

namespace HRRazorForms.Models
{



    public class Position
    {
        public int PositionID { get; set; }
        [StringLength(20, MinimumLength=3)]
        public string name { get; set; }
        public int yearsExperienceRequired { get; set; }
        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
    }

    public class Applicant
    {
        public int ApplicantId { get; set; }
        [StringLength(20, MinimumLength = 3)]
        public string name { get; set; }
        public string telephone { get; set; }
        public string skypeuser { get; set; }
        public ApplicantImage photo { get; set; }
        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }

    }

    public class ApplicantPosition
    {
        public int ApplicantID { get; set; }
        public int PositionID { get; set; }
        public virtual Position Position { get; set; }
        public virtual Applicant Applicant { get; set; }
        public DateTime appliedDate { get; set; }
        public int StatusValue { get; set; }

        public Status Status
        {
            get { return (Status)StatusValue; }
            set { StatusValue = (int)value; }
        }

        //[NotMapped]
        //public int numberOfApplicantsApplied
        //{
        //    get
        //    {
        //        int query =
        //             (from ap in Position
        //              where ap.Status == (int)Status.Applied
        //              select ap
        //                  ).Count();
        //        return query;
        //    }
        //}
    }




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

    public class Address
    {
        [StringLength(20, MinimumLength = 3)]
        public string Country { get; set; }
        [StringLength(20, MinimumLength = 3)]
        public string City { get; set; }
        [StringLength(20, MinimumLength = 3)]
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }    
    }



    public class ApplicationPositionHistory
    {
        public ApplicantPosition applicantPosition { get; set; }
        public Status oldStatus { get; set; }
        public Status newStatus { get; set; }
        [StringLength(500, MinimumLength = 10)]
        public string comments { get; set; }
        public DateTime dateModified { get; set; }
    }

    public enum Status
    {
        Applied,
        AcceptedByHR,
        AcceptedByTechnicalDepartment,
        InterviewedByHR,
        InterviewedByTechnicalDepartment,
        InterviewedByGeneralManager,
        AcceptedByGeneralManager,
        NotAccepted
    }



}
Run Code Online (Sandbox Code Playgroud)

nem*_*esv 11

如果调用属性或(或者使用Key属性注释),EF Code First只能推断属性是主键.因此,您需要使用ApplicantImageId或Id属性等扩展您的例如ApplicantImage.Id<class name>Id

编辑:关于coneventions的一个artice:Code First的约定