5

を定義していませんか?それは正しい?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と呼ばれている場合(またはそれがキー属性で注釈されている場合)。 など。 ApplicantImageIdまたはIDプロパティを持つApplicantImageなど

編集:coneventionsについてartice:Conventions for Code First

+1

私はそれのためにテーブルを作成したいdidntはので、私は申請者クラスに画像を移動しました。そして、必要に応じて[Key]を明示します。今それは正常に動作します。ありがとう –

2

あなたが財産ApplicantId[Key] atributteを追加したり、メソッドDbContext

modelBuilder.Entity<ApplicantImage >().HasKey(p => p.ApplicantId); 
をOnModelCreatingオーバーライド流暢API経由でそれを行うことができますあなたのケースでは
1

、EFの命名規則は、最初のID(大文字と小文字を区別しない)列を探します。何もない場合は、ApplicantImageIdを探して何も見つからない場合は、そのエラーが発生します。

だから、あなたはあなたのIDに[キー]属性を追加する必要があります

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

ApplicantId列がIDがデータベース内にある場合、あなたはあまりにも別の属性を追加する必要があります

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

Iをこれは古い質問であることは知っていますが、それはまだ関連しています。私は同じ状況に遭遇しましたが、.ttファイルを使用してedmxから.csファイルを生成しました。私たちの.ttは、ほとんどの状況でテーブルの最初の列に[Key]属性を追加するように設定されていますが、私の場合、SQLの行over()を使って最初の列の一意のIDを生成していました状況)。その問題は、それがnullを可能にし、.ttがこのケースで[Key]を追加するようにセットアップされていないことでした。 ISNULLに)(行をオーバーラッピング

(()、0)列がNULLでない作る修正することができたと私の問題を解決しました。さもなければ、marianoszが述べたように、データコンテキストで.HasKey()を使うだけでうまくいくでしょう。

関連する問題