我正在创建一个EF Code-First MVC应用程序。当我第一次开始时,我首先使用我知道需要的表格创建了数据库。现在我遇到了需要添加另一个表的情况。在过去,我通常会使用EF Database-First,只需在SSMS中创建表并更新我的EDMX,但我想扩展我的知识并开始执行Code-First项目。
因此,我需要创建的表名为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");
}
我的问题(如果您没有阅读评论),ApplicantInformation_ID
来自哪里?我显然没有把这个属性放在我的Event
课程中?
感谢任何帮助。
更新
所以我可以摆脱我ICollection<Event>
课程中的ApplicantInformation
和构造函数,它仍然是一对多的关系?
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 :(得分:0)
Per Ivan在评论部分:
我理解,但是你可能把错误的收藏放在了错误的地方。规则很简单 - 您将集合导航属性放在一侧,并在多方面引用导航属性。你描述所需关系的方式,该集合应该是公共ICollection申请人{get;组;在Event类中。
这正是我的问题。