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

时间:2013-11-07 17:44:07

标签: c# asp.net asp.net-mvc-4

我正在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,表格只是空的。关联和导航属性似乎设置正确,但由于我是新手,我不是百分之百确定。

1 个答案:

答案 0 :(得分:0)

好的,所以我偶然发现了答案。我删除并重新创建了数据库,当它再次运行Seed()时,它会填充我应该的所有表。最初,我正在对Seed()进行更改并进行更新,希望它们能够应用。但由于数据已存在于表中,因此并未填充。