How do I use Entity Framework in Code First Drop-Create mode?

Use DropCreateDatabaseAlways initializer for your database. It will always recreate database during first usage of context in app domain:

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

Actually if you want to seed your database, then create your own initializer, which will be inherited from DropCreateDatabaseAlways:

public class MyInitializer : DropCreateDatabaseAlways<YourContextName>
     protected override void Seed(MagnateContext context)
         // seed database here

And set it before first usage of context

Database.SetInitializer(new MyInitializer());

Leave a Comment