创建新 Table EF Code-First 一对多的困惑

Confusion on Creating New Table EF Code-First One to Many

我正在创建一个 EF Code-First MVC 应用程序。刚开始时,我首先使用我知道需要的 table 创建了数据库。现在我遇到了需要添加另一个 table 的情况。在过去,我通常首先执行 EF 数据库并仅在 SSMS 中创建 table 并更新我的 EDMX,但我想扩展我的知识并开始执行项目代码优先。

所以我需要创建的 table 称为 Event 并且 table 将链接到另一个已经创建的 table.. 称为 ApplicantInformation.

关系是1个Event可以有多个申请人信息- Event 1 ... * ApplicantInformation.

这是我创建 Event class 的方式:

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

这是我的 ApplicantInformation class:

[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");
    }

我的问题(如果您没有阅读评论),ApplicantInformation_ID 是从哪里来的?我显然没有把那个 属性 放在我的 Event class?

感谢任何帮助。

更新

所以我可以去掉 ApplicantInformation class 中的 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");
    }

Per Ivan 在评论部分:

I understand, but then you've probably put the wrong collection on the wrong place. The rule is simple - you put the collection navigation property at one side and reference navigation property at many side. The way you described the desired relationship, the collection should be public ICollection Applicants { get; set; } inside the Event class.

这正是我的问题。