How do you enable database Seeding?

Jan 22, 2013 at 11:02 PM

I am fairly new to C#, MVC, etc., but I'm so glad Project Silk is available for learning!

I used the Silk code as a guide for a new project, so my project is almost exactly the same. I am using the same repository initializer code and also using the DropCreateIfModelChangesSqlCeInitializer.

My problem is that I absolutely cannot get any Seed data to fill in the database! The database creates, as expected, and I can view it, and I can add data to it just fine. But no matter if I delete the .sdf database file and let Entity recreate it or if I change to the DropCreateAlwaysSqlCeInitializer, it never uses the Seed() method to add sample data. I copied over the code to Seed the Countries, so that's in the DbContextSeedData.cs file. And I can see in the DropCreateIfModelChangesSqlCeInitializer.cs file that it is calling this.Seed(context); but it is absolutely not seeding the database.

Can anyone help point me in the right direction here please? What I missing to cause the Seed() method to happen?

(if I didn't provide some necessary information, please let me know! Sorry if I did.)

Jan 23, 2013 at 1:31 PM
Edited Jan 23, 2013 at 1:34 PM


Have you checked if you are setting the corresponding IDatabaseInitializer when the application starts by calling the Database.SetInitializer method in Global.asax.cs - Application_Start(). For example in MilageStats this can be seen in the constructor of the RepositoryInitializer class:

Database.SetInitializer(new DropCreateIfModelChangesSqlCeInitializer<MileageStatsDbContext>());

Which is run, when calling the InitializeDatabase method in Application_Start() of the Global.asax.cs file.

Also, regarding this topic I believe you could also find the following step by step tutorial interesting:

I hope this helps,

Agustin Adami