Showing posts with label migrations. Show all posts
Showing posts with label migrations. Show all posts

Wednesday, January 22, 2014

Database Migrations in Entity Framework Code First

Enabling Migrations

Run the Enable-Migrations command in Package Manager Console

Generating & Running Migrations

Add-Migration migration_name

Update-Database –Verbose

Update-Database –TargetMigration: migration_name

If you want to roll all the way back to an empty database then you can use

Update-Database –TargetMigration: $InitialDatabase

Getting a SQL Script

Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: migration_name



Automatically Upgrading on Application Startup (MigrateDatabaseToLatestVersion Initializer)

When we create an instance of this initializer we need to specify the context type (MyDbContext) and the migrations configuration (Configuration) - the migrations configuration is the class that got added to our Migrations folder when we enabled Migrations.

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());



References:
http://msdn.microsoft.com/en-us/data/jj591621.aspx