加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – EntityType’ApplyantPosition’没有定义键

发布时间:2020-12-16 03:41:31 所属栏目:asp.Net 来源:网络整理
导读:运行我的第一个asp.net mvc应用程序时出现此错误 我认为实体框架会自动创建以Id结尾的列名的键吗?不正确吗? 如您所见,ApplicantPositionID将是一个包含2列作为主键的表,因为它与申请人和职位相关. 在模型生成期间检测到一个或多个验证错误: System.Data.E
运行我的第一个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
    }



}

解决方法

如果属性名为Id或< class name> Id(或者如果使用Key属性注释),则EF Code First只能推断属性是主键.
所以你需要扩展你的具有ApplicantImageId或Id属性等的ApplicantImage.

编辑:关于coneventions的一个artice:Conventions for Code First

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读