未调用实体框架种子

时间:2016-05-11 13:39:30

标签: c# entity-framework

我遇到数据库初始化问题 - 从不调用种子方法。类似的代码在另一个项目中工作,所以我很困惑为什么他们这次不工作。

这是我的代码:

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();
      }
    }

RecipeInitialiser.cs

    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);
    }
}

的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文件中输入了一个拼写错误

我尝试了什么

  • 在种子方法中设置断点,该方法显示从不调用种子方法
  • 我试过看Stack Overflow herehere,但我无法看到我出错的地方

1 个答案:

答案 0 :(得分:2)

好像你有一个错字。

您的初始化程序类名称是

RecipeInitialiser

在配置中你有

<databaseInitializer type="WebRecipes.DataLayer.Models.RecipeInitializer, WebRecipes.DataLayer" />