NRKirby
NRKirby

Reputation: 1634

Entity Framework seed not being called

I am having problems with a database initialiser - the seed method is never called. Similar code worked in another project so I'm confused why they aren't working this time.

Here is the code I have:

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>

Why this post is different to the possible duplicate:

What I have tried

Upvotes: 3

Views: 613

Answers (1)

Leandro Gabriel Casas
Leandro Gabriel Casas

Reputation: 91

It seems you have a typo.

You initializer class name is

RecipeInitialiser

And in the config you have

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

Upvotes: 3

Related Questions