5

當運行我的第一個asp.net mvc應用程序時,我得到了這個錯誤 我認爲實體框架會自動創建以ID結尾的列名的鍵?它不正確嗎?EntityType'ApplicantPosition'沒有定義的鍵

正如您所見,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 
    } 



} 

回答

11

EF代碼首先只能推斷一個屬性是主鍵如果該屬性被調用Id<class name>Id(或者如果它注有關鍵屬性)。 所以你需要擴展你的例如ApplicantImage與ApplicantImageId或id屬性等

編輯:關於coneventions的條每頁:Conventions for Code First

+1

我將圖像移動到申請人類,因爲我不想爲它創建表。我在需要的地方明確地[Key]。現在它工作正常。謝謝 –

2

您可以將[Key] atributte添加到屬性ApplicantId或通過流暢API做覆蓋OnModelCreating方法的DbContext

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

就你而言,EF命名約定首先查找ID(不區分大小寫)列。如果沒有,尋找ApplicantImageId,當它什麼都沒找到時,它會引發這個錯誤。

所以,你應該添加[關鍵]屬性上您的ID:

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; } 
} 
0

我知道這是一個古老的問題,但它仍然是相關的。我遇到了同樣的情況,但是我們使用.tt文件來從我們的edmx生成.cs文件。我們的.tt被設置爲在大多數情況下在表的第一列添加[Key]屬性,但在我的情況下,我在SQL中使用over()來爲第一列生成唯一的ID(對大多數人很有用的情況下)。問題在於它使得可以爲空,並且.tt在這種情況下未被設置爲添加[Key]。

在ISNULL((),0)中包裝行Over()能夠修復使列不爲空並解決我的問題。否則,正如marianosz所述,只需在數據上下文中使用.HasKey()也可以。

相關問題