2017-08-16 176 views
-1

我正在創建EF Code-First MVC應用程序。當我第一次開始時,我首先用我知道我需要的表格創建數據庫。現在我遇到了需要添加另一個表格的情況。在過去,我通常先做EF Database-First,然後在SSMS中創建表格並更新我的EDMX,但我想擴展我的知識並開始執行Code-First項目。創建新表EF編碼的困惑 - 首先一對多

因此,我需要創建的表格被稱爲Event,該表格將鏈接到已創建的另一個表格,稱爲ApplicantInformation

關係是1事件可以有許多申請人信息 - Event 1 ... * ApplicantInformation

下面是我創建了我Event類:

public class Event 
{ 
    [Key] 
    public int Id { get; set; } 
    public string EventName { get; set; } 
    public bool Active { get; set; } 
} 

這裏是我ApplicantInformation類:

[Table("ApplicantInformation")] 
public partial class ApplicantInformation 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public ApplicantInformation() 
    { 
     Events = new HashSet<Event>(); 
    } 
    public int ID { get; set; } 

    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    public int City { get; set; } 

    public int State { get; set; } 
    public string EmailAddress { get; set; } 

    public bool Active { get; set; } 
    public DateTime DateEntered { get; set; } 
    public int EventId { get; set; } 

    [ForeignKey("EventId")] 
    public Event Event { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Event> Events { get; set; } 
} 

當我add-migration我看到這一點:

public override void Up() 
    { 
     CreateTable(
      "dbo.Events", 
      c => new 
       { 
        Id = c.Int(nullable: false, identity: true), 
        EventName = c.String(), 
        Active = c.Boolean(nullable: false), 
        ApplicantInformation_ID = c.Int(), // where does this come from? 
       }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.ApplicantInformation", t => t.ApplicantInformation_ID) // where did ApplicantInformation_ID come from? 
      .Index(t => t.ApplicantInformation_ID); // again, where did this property come from? 

     AddColumn("dbo.ApplicantInformation", "EventId", c => c.Int(nullable: false)); 
     CreateIndex("dbo.ApplicantInformation", "EventId"); 
     AddForeignKey("dbo.ApplicantInformation", "EventId", "dbo.Events", "Id", cascadeDelete: true); 
     DropColumn("dbo.ApplicantInformation", "CareerEvent"); 
    } 

我的問題(如果你沒有閱讀評論),是在哪裏從哪裏來?我顯然沒有把這個屬性放在我的Event類中?

任何幫助表示讚賞。

UPDATE

這樣我就可以得到我的ApplicantInformation類擺脫ICollection<Event>和構造的,它仍然是一個一對多的關係?

public class Event 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public Event() 
    { 
     ApplicantInformations = new HashSet<ApplicantInformation>(); 
    } 

    [Key] 
    public int Id { get; set; } 
    public string EventName { get; set; } 
    public bool Active { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<ApplicantInformation> ApplicantInformations { get; set; } 
} 

[Table("ApplicantInformation")] 
public partial class ApplicantInformation 
{ 
    public int ID { get; set; } 

    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    public int City { get; set; } 

    public int State { get; set; } 
    public string EmailAddress { get; set; } 

    public bool Active { get; set; } 
    public DateTime DateEntered { get; set; } 
    public int EventId { get; set; } 

    [ForeignKey("EventId")] 
    public Event Event { get; set; } 
} 

遷移:

public override void Up() 
    { 
     CreateTable(
      "dbo.Events", 
      c => new 
       { 
        Id = c.Int(nullable: false, identity: true), 
        EventName = c.String(), 
        Active = c.Boolean(nullable: false), 
       }) 
      .PrimaryKey(t => t.Id); 

     AddColumn("dbo.ApplicantInformation", "EventId", c => c.Int(nullable: false)); 
     CreateIndex("dbo.ApplicantInformation", "EventId"); 
     AddForeignKey("dbo.ApplicantInformation", "EventId", "dbo.Events", "Id", cascadeDelete: true); 
     DropColumn("dbo.ApplicantInformation", "CareerEvent"); 
    } 
+0

它來自公共虛擬ICollection Events {get;組; }'ApplicantInformation'實體的導航屬性。在那裏添加該房產的任何理由?因爲它定義了另一個關係 - 「申請人信息1 ... *事件」。 –

+0

@IvanStoev我添加的原因是因爲我最初在db中創建了其他表,然後當我選擇使用EF Code-First並將錶帶過來時..我有使用該行的類..例如..在我的其他課程之一,我有'公開虛擬ICollection 申請人信息{獲得;組; }'因爲這個其他表與申請人信息表 –

+0

有關我的更新請參閱我已更正的信息 –

回答

0

每伊萬在評論部分:

我明白,但你可能已經把錯誤的收集上錯了地方。規則很簡單 - 您將集合導航屬性放在一側,並在多側放置參考導航屬性。您描述期望的關係的方式,收集應該是公衆ICollection Applicants {get;組;在Event類中。

這正是我的問題。