Reputation: 31
I am admittedly going a little bit crazy on the subject, so bear with me. I've been attempting to do this for about 5 hours now, and I'm pretty much just banging my head against the wall at this point. Here is what I have:
Global.asax.cs
namespace MyApp
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer(new MyInitializer());
}
}
}
MyInitializer.cs
namespace MyApp.App_Start
{
public class MyInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
protected override void Seed(MyContext context)
{
// seed data goes here
sample_data = new List<MyModel>
{
new MyModel{"foo"},
new MyModel{"bar"}
};
sample_data.ForEach(s => context.MyModels.Add(s));
context.SaveChanges();
}
}
}
MyContext.cs
namespace MyApp.Models
{
public class MyContext : DbContext
{
public MyContext() : base("MyContext")
{
Database.SetInitializer<MyContext>(new MyInitializer());
}
public DbSet<MyModel> MyModels { get; set; }
}
}
Web.config
<connectionStrings>
<add name="MyContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=MyContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|MyContext.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<contexts>
<context type="MyApp.Models.MyContext, MyApp">
<databaseInitializer type="MyApp.App_Start.MyInitializer, MyApp" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
I just cannot figure out why it's not seeding any of the data. I've read through the dozen or more similar cases here and elsewhere, tried just about every iteration, and still can't get it to trigger at all. It never even hits the Seed() function. The database exists, and if I remove it, it is rebuilt, but never populated.
I'm leaning towards there being a problem with my web.config, but I haven't figured it out yet. Any suggestions?
Upvotes: 2
Views: 834
Reputation: 31
I resolved this. Apparently, the issue was two-fold.
The "DropCreateDatabaseIfModelChanges" in the initializer wasn't firing, as it didn't see that the model had changed. Changing this to "DropCreateDatabaseAlways" worked, but would usually result in a "Cannot drop database because it is currently in use" error message. A manual disconnect of databases and restart of VS usually resolved this, though I still usually had to run it 2-3 times. Once it dropped and rebuilt the database, reverting to "DropCreateDatabaseIfModelChanges" is faster and still works.
I may have been missing a line in my Web.config
<appSettings>
<add key="MyInitializer MyApp.Models.MyContext, MyApp.Models" value="MyApp.App_Start.MyInitializer, MyApp.Models" />
</appSettings>
Between these two things [well, more like 4 things], it seems to work now.
Upvotes: 1
Reputation: 7638
According to this blog, the DbInitializer isn't called until you actually try to access the database.
// Code copied from linked blog
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<BlogContext>());
using (var db = new BlogContext()) //initializer won't be called here
{
...
db.Categories.Add(cat); //initializer will be called here
db.BlogPosts.Add(post);
...
}
Console.ReadLine();
}
Upvotes: 1