3

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:

  • As pointed out by Leandro Gabriel Casas I did have a typo in in my Web.config file

What I have tried

  • Set a breakpoint at the seed method which shows the seed method is never called
  • I have tried looking on Stack Overflow here and here but I haven't been able to see where I'm going wrong
Community
  • 1
  • 1
NRKirby
  • 1,584
  • 4
  • 21
  • 38
  • How / when are you running the initialiser? You can either wait for the first use of context to fire the initialiser or you can do it manually in your Application_Start() method. – Paul Zahra May 11 '16 at 13:57
  • Possible duplicate of [DropCreateDatabaseAlways Seed not called](http://stackoverflow.com/questions/14242699/dropcreatedatabasealways-seed-not-called) – Paul Zahra May 11 '16 at 13:58
  • @NRKirby it was me lol but deleted it... as i wasnt sure how that config works... – Seabizkit May 11 '16 at 13:58
  • Sorry to add a comment so late but it would help if you'd mark the answer as correct, after all, you say in the main post, you had a typo in the web config file. – Leandro Gabriel Casas Jan 16 '20 at 01:01

1 Answers1

3

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" />