Drop or Recreate database with Entity Framework Migrations (Code-First)

.net asp.net ef-migrations entity-framework

Question

Which is the command to recreate or drop the database when using Entity Framework Migrations, not Initializers?

What should I write on the package manager console?

COMMENT:

I'm looking for some command that gives me the same functionality as Database.SetInitializer<>(new DropCreateDatabaseIfModelChanges<>()); but with the migrations approach.

1
9
2/1/2014 5:29:57 AM

Popular Answer

You can get the same behavior with Migrations by using automatic migrations.

PM> enable-migrations -EnableAutomaticMigrations

In Configuration.cs in the constructor, make sure automatic migrations and allow data loss are set to true...

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

Now whenever your model changes, just execute update-database...

PM> update-database

The schema will be changed to match the models and any existing data in the tables will stay there (unless it's in a renamed or removed column). The Seed method is run after the database is updated so you can further control any changes to the data in there.

8
1/31/2014 11:58:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow