Friday, December 6, 2013

DataMigration in MVC and EF

I tinkered around with Data Migrations in MVC 4.0 and I have to say, It's really convenient.


Make sure connection string is updated to the desired database

In NuGet package manager console
PM> Enable-Migrations -ContextTypeName Class1

  • Class1 derives from DbContext
  • -ContextTypeName is only needed if more than one class derives from DbContext

Note that some files are added to the Migrations folder
  • configuration.cs
    • Set  AutomaticMigrationsEnabled = true  to update database for development
    • Seed(...) is used to update the database
  • migration.cs
    • The seed(...) method in Configuration is ran everytime update-database command is issued
To update the database
PM> update-database

No comments:

Post a Comment