EntityType“ApplicantPosition”没有定义键

2023-11-21

运行我的第一个 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.

错误在这一行抛出:

public ActionResult Index()
        {
            return View(db.Positions.ToList());
        }

我的模型如下:

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
    }



}

EF Code First 只能推断属性是首要的关键如果该属性被称为Id or <class name>Id(或者如果它用 Key 属性注释)。 所以你需要扩展你的例如具有 ApplicantImageId 或 Id 属性等的 ApplicantImage

编辑:一篇关于约定的文章:Code First 约定

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EntityType“ApplicantPosition”没有定义键 的相关文章

随机推荐