1

I am trying to seed my application with some test data via EF Code First, when the app starts up.

Here is what I am observing:

When I run the app, execution goes into:

Application_Start() {}

And then once in Application_Start() it lands on:

Database.SetInitializer<LocatorContext>(new DropCreateDatabaseAlways<LocatorContext>());

But it never makes it into the DBContext class (ClubLocatorContext.cs), which is where the seeding happens, among other things.

Any thoughts as to why or how I can fix it would be much appreciated.

My Global.asax.cs File

//Global.asax.cs
protected void Application_Start()
{
    Database.SetInitializer<LocatorContext>(new DropCreateDatabaseAlways<LocatorContext>());

    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

My DBContext Class

//ClubLocatorContext.cs        
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using ClubLocator.Models;
using ClubLocator.Models.ViewModels;

namespace ClubLocator.DAL
{
    public class LocatorContext : DbContext
    {

      public DbSet<Prospect> Prospects { get; set; }

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

      public void Seed(LocatorContext context)
      {
          var prospect = new List<Prospect>
                         {
                              new Prospect
                              {
                                      FirstName = "John",
                                      LastName = "Smith",
                                      Address1 = "1313 Mockingbird Lane",
                                      Email = "[email protected]"
                              }
                         };

          prospect.ForEach(r => context.Prospects.Add(r));
          context.SaveChanges();
      }

      public class DropCreateIfChangeInitializer : DropCreateDatabaseIfModelChanges<LocatorContext>
      {
          protected override void Seed(LocatorContext context)
          {
              context.Seed(context);
              base.Seed(context);
          }
      }

      public class CreateInitializer : DropCreateDatabaseAlways<LocatorContext>
      {
          protected override void Seed(LocatorContext context)
          {
              context.Seed(context);
              base.Seed(context);
          }
      }

      static LocatorContext()
      { 
          #if DEBUG
          Database.SetInitializer<LocatorContext> (new DropCreateIfChangeInitializer ());
          #else
          Database.SetInitializer<LocatorContext> (new CreateInitializer ());
          #endif
      }
    }
}
2
  • It's just setting initializer, not initialized. When you access the database it will create the database and seed data. Commented Jun 20, 2013 at 23:51
  • Jason, Thanks. What would that look like from inside Application_Start():? Commented Jun 21, 2013 at 1:25

1 Answer 1

1
new DropCreateIfChangeInitializer()
                .InitializeDatabase(new LocatorContext());
Sign up to request clarification or add additional context in comments.

6 Comments

Jason, I see that method in my context class but intellisense in VS doesn't like that syntax. Can you show me what it should look like from inside Application_Start()? Thanks
You created DropCreateIfChangeInitializer which extends DropCreateDatabaseIfModelChanges<LocatorContext> which has public method InitializeDatabase. So I don't understand why you can't invoke the method in Application_Start
It's weird. The only 2 methods that come up in intellisense are DropCreateDatabaseAlways<> and DropCreateDatabaseIfModelChanges<>
protected void Application_Start() { new DropCreateIfChangeInitializer() .InitializeDatabase(new LocatorContext());}
If still not work, you can paste your code in application_start.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.