-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类中。

这正是我的问题。