我正在创建一个 EF Code-First MVC 应用程序。当我第一次开始时,我首先使用我知道需要的表创建数据库。现在我遇到了一种情况,我需要添加另一个表。过去,我通常使用 EF 数据库优先,只是在 SSMS 中创建表并更新我的 EDMX,但我想扩展我的知识并开始代码优先做项目。
因此,我需要创建的表称为
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");
}
评论部分的每伊万:
我明白,但你可能把错误的收藏放在了错误的地方。规则很简单 - 将集合导航属性放在一侧,将引用导航属性放在多侧。按照您描述所需关系的方式,该集合应该是 public ICollection Applicants { get;放; } 在 Event 类中。
这正是我的问题。