0

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.

3
  • Have you called Database.SetInitializer for your context? Apr 1, 2013 at 12:55
  • Ha no I hadn't, I can't believe I forgot that. Thanks Nicholas. But the problem is still not solved. I am now getting the error cannot convert from Domain.BSContext to System.Data.Entity.IDatabaseInitializer<Domain.BSContext> I placed the initalizer code in my Application_Start() method like so Database.SetInitializer<BSContext>(new BSContext());
    – J86
    Apr 1, 2013 at 13:07
  • no problem - we all do it! I've added the correct syntax as an answer. Apr 1, 2013 at 13:29

1 Answer 1

0

You need to call Database.SetInitializer like this:

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.