C# 未调用实体框架种子

C# 未调用实体框架种子,c#,entity-framework,C#,Entity Framework,我在使用数据库初始化器时遇到问题-从未调用seed方法。类似的代码在另一个项目中工作,所以我不明白为什么这次它们不工作 以下是我的代码: RecipeContext.cs public class RecipeContext : DbContext { public RecipeContext() : base("DefaultConnection") { } public DbSet<Recipe> Recipes

我在使用数据库初始化器时遇到问题-从未调用seed方法。类似的代码在另一个项目中工作,所以我不明白为什么这次它们不工作

以下是我的代码:

RecipeContext.cs

    public class RecipeContext : DbContext
    {
      public RecipeContext() : base("DefaultConnection")
      {
      }

      public DbSet<Recipe> Recipes { get; set; }

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
          base.OnModelCreating(modelBuilder);
      }

      public static RecipeContext Create()
      {
          return new RecipeContext();
      }
    }
    public class RecipeInitialiser : DropCreateDatabaseAlways<RecipeContext>
    {
      protected override void Seed(RecipeContext context)
      {
        var recipes = new List<Recipe>
        {
            new Recipe {
                Name = "Spag Bol",
                Ingredients = "1. This\n2. That\n3. This too",
                Method = "1. Do this\n2. Do that\n3. And this too",
                PrepTime = new TimeSpan(0, 45, 0),
                CookTime = new TimeSpan(1, 45, 0),
                Difficulty = 1,
                Serves = 6,
                DateCreated = DateTime.Now
            },
          };

          recipes.ForEach(r => context.Recipes.Add(r));
          context.SaveChanges();
          base.Seed(context);
    }
}
公共类RecipeContext:DbContext { 公共RecipeContext():基本(“DefaultConnection”) { } 公共数据库集配方{get;set;} 模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove(); 基于模型创建(modelBuilder); } 公共静态RecipeContext Create() { 返回新的RecipeContext(); } } RecipeiInitializer.cs

    public class RecipeContext : DbContext
    {
      public RecipeContext() : base("DefaultConnection")
      {
      }

      public DbSet<Recipe> Recipes { get; set; }

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
          base.OnModelCreating(modelBuilder);
      }

      public static RecipeContext Create()
      {
          return new RecipeContext();
      }
    }
    public class RecipeInitialiser : DropCreateDatabaseAlways<RecipeContext>
    {
      protected override void Seed(RecipeContext context)
      {
        var recipes = new List<Recipe>
        {
            new Recipe {
                Name = "Spag Bol",
                Ingredients = "1. This\n2. That\n3. This too",
                Method = "1. Do this\n2. Do that\n3. And this too",
                PrepTime = new TimeSpan(0, 45, 0),
                CookTime = new TimeSpan(1, 45, 0),
                Difficulty = 1,
                Serves = 6,
                DateCreated = DateTime.Now
            },
          };

          recipes.ForEach(r => context.Recipes.Add(r));
          context.SaveChanges();
          base.Seed(context);
    }
}
公共类RecipeInitializer:DropCreateDatabaseAlways
{
受保护的覆盖无效种子(RecipeContext上下文)
{
var recipes=新列表
{
新配方{
Name=“Spag Bol”,
配料=“1.这个\n2.那个\n3.这个也”,
Method=“1.做这个\n2.做那个\n3.还有这个”,
PrepTime=新的时间跨度(0,45,0),
烹饪时间=新的时间跨度(1,45,0),
难度=1,
发球=6,
DateCreated=DateTime.Now
},
};
recipes.ForEach(r=>context.recipes.Add(r));
SaveChanges();
种子(上下文);
}
}
Web.config

<entityFramework>
  <contexts>
    <context type="WebRecipes.DataLayer.RecipeContext, WebRecipes.DataLayer">
      <databaseInitializer type="WebRecipes.DataLayer.Models.RecipeInitializer, WebRecipes.DataLayer" />
    </context>
  </contexts>
  ...other_settings
</entityframework>

…其他设置
此帖子与可能重复的帖子不同的原因:
  • 正如Leandro Gabriel Casas所指出的,我的Web.config文件中确实有一个输入错误
我试过的
  • 在seed方法处设置断点,该断点显示从未调用seed方法
  • 我已经试着查看堆栈溢出,但我还没有看到哪里出了问题

你好像有打字错误

初始值设定项类名为

RecipeiInitializer

在你的配置中


如何/何时运行初始化器?您可以等待第一次使用上下文触发初始化器,也可以在应用程序_Start()方法中手动执行。可能是@nrkiby的重复,它是我的lol,但已将其删除。。。因为我不知道该配置是如何工作的……很抱歉这么晚才添加评论,但如果你能将答案标记为正确,那会有所帮助,毕竟,你在主要帖子中说,你在web配置文件中有一个输入错误。