Search code examples
asp.net-mvcasp.net-mvc-4entity-framework-4seeding

Can't get Seeding to Work EF Code First


I have a couple of related entities and I am trying to seed the database with some dummy data. Here is my seed code:

public class EventInitializer : DropCreateDatabaseAlways<BSContext>
{
    protected override void Seed(BSContext context)
    {
        var authors = new List<Author>
        {
            new Author { Name = "Christina Gabbitas" },
            new Author { Name = "Gemma King" },
            new Author { Name = "Gemma Collins"},
            new Author { Name = "Billy Hayes" },
            new Author { Name = "Jodi Picoult" },
            new Author { Name = "John Whaite" }
        };
        authors.ForEach(a => context.Authors.Add(a));
        context.SaveChanges();

        var events = new List<Event>
        {
            new Event { Authors = new List<Author> { context.Authors.Find(0) }, Book = "Felicity Fly", Info = "Christina Gabbitas will be signing copies of her new book, Felicity Fly. Books should be bought from WHSmith. Proof of purchase may be necessary", Start = new DateTime(2013, 05, 25, 10, 30, 00), Url = "http://www.whsmith.co.uk/Support/InStoreSignings.aspx", Location = new Location { Name = "WHSmith Brent Cross", Address = "Brent Cross Shopping Centre", City = "London", County = "", PostCode = "NW4 3FB", Telephone = 02082024226 } },
            new Event { Authors = new List<Author> { context.Authors.Find(1) }, Book = "Haunted Spalding", Info = "Gemma King will be signing copies of her new book. Books should be bought from WHSmith. Proof of purchase may be necessary", Start = new DateTime(2013, 03, 31, 10, 00, 00), Url = "http://www.whsmith.co.uk/Support/InStoreSignings.aspx", Location = new Location { Name = "WHSmith Spalding", Address = "6-7 Hall Place", City = "Spalding", County = "Lincolnshire", PostCode = "PE11 1SA", Telephone = 01775768666 } },
            new Event { Authors = new List<Author> { context.Authors.Find(3) }, Book = "Midnight Express", Info = "Billy Hayes will be signing copies of his books. Books should be bought from WHSmith. Proof of purchase may be necessary", Start = new DateTime(2013, 04, 13, 13, 00, 00), Url = "http://www.whsmith.co.uk/Support/InStoreSignings.aspx", Location = new Location { Name = "WHSmith Birmingham", Address = "29 Union Street", City = "Birmingham", County = "West Midlands", PostCode = "B2 4LR", Telephone = 01216313303 } }
        };
        events.ForEach(e => context.Events.Add(e));
        context.SaveChanges();
    }
}

The seed code above sits in a separate project along with all my entities. I did this to keep my domain model totally separate from my web application. Of course I have references in my controllers to access the entities.

I've used EF Code First before, but this time it isn't working for me! When I go to access the data like so in my controller (ASP.NET MVC application), I get 0 results.

public ActionResult Index()
{
    ViewBag.Message = "Move around the map to find events near you.";

    var model = new IndexVM();

    using(var context = new BSContext())
    {
        model.Events = (List<Event>)context.Events.ToList();
    }

    return View(model);
}

I am using EF (v4.0.30319) on Windows 8 64x Pro with Visual Studio 2012. To make matters worse, I can't even debug! My breakpoint is never hit when I try to run in debug mode! Here is my Web.config for the web project.


Solution

  • You need to call Database.SetInitializer like this:

    Database.SetInitializer<BSContext>( new EventInitializer() );