实体类型“申请人职位”没有定义键

发布于 2024-12-10 03:40:15 字数 3819 浏览 0 评论 0原文

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



}

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 correct?

As you can see the ApplicantPositionID would be a table with 2 columns as primary key because it would relate to Applicants and also to Position.

One or more validation errors were detected during model generation:

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.

The error is thrown in this line:

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

And my model is the following one:

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
    }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空宴 2024-12-17 03:40:15

仅当属性名为 IdId(或者如果它用 Key 属性注释)。
因此,您需要使用 ApplicantImageId 或 Id 属性等来扩展您的 ApplicantImage。

编辑:有关约定的文章: 代码优先约定

EF Code First can only infer that a property is a primary key if the property is called Id or <class name>Id (or if it is annotated with the Key attribute).
So you need to extend your e.g. ApplicantImage with an ApplicantImageId or Id property etc.

Edit: An artice about the coneventions: Conventions for Code First

爱殇璃 2024-12-17 03:40:15

您可以将 [Key] 属性添加到属性 ApplicantId 或通过 Fluent API 重写 OnModelCreating 方法 DbContext 来完成

modelBuilder.Entity<ApplicantImage >().HasKey(p => p.ApplicantId);

You can add the [Key] atributte to the property ApplicantId or do it via Fluent API overriding OnModelCreating method DbContext

modelBuilder.Entity<ApplicantImage >().HasKey(p => p.ApplicantId);
尬尬 2024-12-17 03:40:15

在您的情况下,EF 命名约定首先查找 ID(不区分大小写)列。如果没有找到,则查找 ApplicantImageId ,当找不到任何内容时,会引发该错误。

因此,您应该在 ID 上添加 [Key] 属性:

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

如果 ApplicantId 列是数据库中的身份,则还应该添加另一个属性:

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

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; }
}
‘画卷フ 2024-12-17 03:40:15

我知道这是一个老问题,但仍然相关。我遇到了同样的情况,但是我们使用 .tt 文件从 edmx 生成 .cs。在大多数情况下,我们的 .tt 设置为在表的第一列上添加 [Key] 属性,但在我的情况下,我在 SQL 中使用 () 上的一行来为第一列生成唯一的 id(对于大多数情况都有效)情况)。问题是它使可为空,并且在这种情况下 .tt 未设置为添加 [Key]。

将行 Over() 包装在 ISNULL ((),0) 中能够修复使列不为空并解决我的问题。否则,正如 marianosz 所提到的,只需在数据上下文中使用 .HasKey() 也可以正常工作。

I know this is an old question but it is still relevant. I ran into the same situation however we use a .tt file to generate the .cs from our edmx. Our .tt is setup to add the [Key] attribute on our first column of the table for most situations, but in my case i was using a row over () in SQL to generate unique id's for the first column (works great for most situations). The problem with that was it makes a nullable and the .tt wasn't setup to add [Key] in this case.

Wrapping the row Over() in a ISNULL ((),0) was able to fix making the column not null and solved my problem. Otherwise, as mentioned by marianosz, simply using the .HasKey() in your data context will work fine too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文