我如何播种具有其他对象列表的对象?

时间:2016-09-21 08:52:05

标签: c# entity-framework entity-framework-6

我有一个比萨课:

public class Pizza
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public List<Ingredient> Ingredients { get; set; }
    public string Image { get; set; }
}

和一个成分类:

public class Ingredient
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<Pizza> Pizza { get; set; }
}

我使用EF 6进行数据迁移,我想为数据库设定种子。唯一的问题是,我该如何处理?我在配置类中有以下代码:

 protected override void Seed(PizzaBoerContext context)
 {
     context.Pizzas.AddOrUpdate(i => i.Name,
         new Pizza
         {
             Name = "Margherita",
             Description = "",
             Image = "PizzaMargherita",
             Ingredients = new List<Ingredient>()
             {
                 new Ingredient() { Name = "Tomatensaus", Description = "Gemaakt van tomaten" },
                 new Ingredient() { Name = "Kaas", Description = "Gemaakt van melk" },
                 new Ingredient() { Name = "Oregano", Description = "Gemaakt van plant" },
             },
             Price = 6.00M
         },
         new Pizza
         {
             Name = "Funghi",
             Description = "",
             Image = "PizzaMargherita",
             Ingredients = new List<Ingredient>()
             {
                 new Ingredient() { Name = "Tomatensaus", Description = "Gemaakt van tomaten" },
                 new Ingredient() { Name = "Kaas", Description = "Gemaakt van melk" },
                 new Ingredient() { Name = "Champions", Description = "Een soort schimmel" },
             },
             Price = 6.00M
         }
         );
      }

请参阅,成分也需要放入数据库中。我把它们放在第一位,还是我可以这样做? EF足够聪明,知道填充成分表吗?我怎样才能避免重复?

更新

我将种子修改为以下代码:

protected override void Seed(PizzaBoerContext context)
    {
        var margharita = new Pizza {
            Name = "Margherita",
            Description = "",
            Image = "PizzaMargherita",
            Ingredients = new List<Ingredient>(),
            Price = 6.00M
        };

        var funghi = new Pizza
        {
            Name = "Funghi",
            Description = "",
            Image = "PizzaFunghi",
            Ingredients = new List<Ingredient>(),
            Price = 8.00M
        };

        var tomatoSauce = new Ingredient() { Name = "Tomatensaus", Description = "Gemaakt van tomaten" };
        var cheese = new Ingredient() { Name = "Kaas", Description = "Gemaakt van melk" };
        var oregano = new Ingredient() { Name = "Oregano", Description = "Gemaakt van plant" };
        var mushroom = new Ingredient() { Name = "Champignon", Description = "Een soort schimmel" };

        margharita.Ingredients.Add(tomatoSauce);
        margharita.Ingredients.Add(cheese);
        margharita.Ingredients.Add(oregano);

        funghi.Ingredients.Add(tomatoSauce);
        funghi.Ingredients.Add(cheese);
        funghi.Ingredients.Add(mushroom);

        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }

唯一的问题是表格是否被填满。我错过了什么?

0 个答案:

没有答案