gpt4 book ai didi

c# - Seed() 没有完全更新数据库

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:00 26 4
gpt4 key购买 nike

我正在浏览一些关于 ASP.NET MVC 的教程(herehere),并决定自己尝试一些东西。现在,我有三个表,ResumeDescriptionsSubDescriptions。这是三者的代码:

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% 确定。

最佳答案

好吧,所以我很偶然地找到了答案。我删除并重新创建了数据库,当它再次运行 Seed() 时,它按预期填充了我的所有表。最初,我正在对 Seed() 进行更改和更新,希望它们能够得到应用。但由于数据已经存在于表中,因此没有填充。

关于c# - Seed() 没有完全更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19842925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com