我正在浏览一些关于 ASP.NET MVC 的教程(这里和这里),并决定自己尝试一些事情。现在,我有三张桌子Resume
,,,,。以下是三者的代码:Descriptions
SubDescriptions
public class Resume
{
public Resume()
{
Descriptions = new List<Description>();
}
[Key]
public int ResumeId { get; set; }
[Required]
public string Employer { get; set; }
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
public DateTime EndDate { get; set; }
[Required]
public string Location { get; set; }
[Required]
public virtual ICollection<Description> Descriptions { get; set; }
}
public class Description
{
public Description()
{
SubDescriptions = new List<SubDescription>();
}
[Key]
public int DescriptionId { get; set; }
[ForeignKey("Resume")]
public int ResumeId { get; set; }
[Required]
public string Desc { get; set; }
public virtual Resume Resume { get; set; }
public virtual ICollection<SubDescription> SubDescriptions { get; set; }
}
public class SubDescription
{
[Key]
public int SubDescriptionId { get; set; }
[ForeignKey("Description")]
public int DescriptionId { get; set; }
[Required]
public string Sub { get; set; }
public virtual Description Description { get; set; }
}
我Seed()
的如下:
protected override void Seed(ResumeDBContext context)
{
context.Resumes.AddOrUpdate(i => i.Employer,
new Resume
{
Employer = "Employer Test",
StartDate = DateTime.Parse("2012-3-26"),
EndDate = DateTime.Parse("2013-10-24"),
Location = "Houston, TX",
Descriptions = { new Description
{ Desc = "DescTest",
SubDescriptions = {new SubDescription {Sub = "SubTest"},
new SubDescription {Sub = "SubTest2"},
new SubDescription {Sub = "SubTest3"}}
},
new Description { Desc = "DescTest2" }}
}
);
}
现在,每当我update-database
从包管理器控制台运行时,它都会说它正在运行Seed()
。但是,在查询数据库时,我的SubDescriptions
表仍然是空的。其他一切都按预期填充。我没有收到任何错误或任何类似的错误。我是否在我的联想中遗漏了一些愚蠢的东西?
该Resume
表已从 中正确填充Seed()
,并且该Descriptions
表也已填充,并具有与该表的适当关联Resume
。然而,按照相同的示例尝试填充SubDescriptions
,该表完全是空的。关联和导航属性似乎设置正确,但由于我是新手,我不能 100% 确定。