Visual-studio – Entity Framework Code First Migration Class File

asp.net-mvc-4databaseentity-framework-5entity-framework-migrationsvisual studio

I'm building an application using ASP.NET MVC 4 with Entity Framework 5.0. I've recently learned about EF Code-First Migrations tool you can implement on your application for your database. I ran "Enable-Migration" in the PM Console, and it enabled successfully and created the "InitialCreate.cs" class as expected with the appropriate up() and down() methods filled in with the table creations.

Then I went ahead and added another class (Category.cs) with 2 simple properties (id, name) and wanted to add this migration to the database. I ran "Add-Migration AddCategoryClass" in my PM to get this message:

Scaffolding migration 'AddCategoryClass'.
The Designer Code for this migration file includes a snapshot of your current Code
First model. This snapshot is used to calculate the changes to your model when you    scaffold the next migration. If you make additional changes to your model that you want to    include in this migration, then you can re-scaffold it by running 'Add-Migration 201307301938256_AddCategoryClass' again.

It created the migration file in the Migrations folder, but when I open up the ".cs" file it created, the Up() and Down() methods are blank. I was expecting it to create a table and add the fields respectively. Out of curiousity, i ran "Update-Database" and it went through successfully (after changing AutomaticMigration = true in the Configuration.cs file). When I built my application, the table appeared in the database.

So my question is –> Why are the Up() and Down() methods blank?

For the record, the "Update-Database" command does not work if AutomaicMigrations = false in the Configuration.cs file; giving me the following error:

Unable to update database to match the current model because there are pending changes     and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

I'm assuming these shouldn't be and it'd be helpful in the future to look at my migrations and see what I did. Any help or guidance would be great. Thank you in advanced.

I'm using VS 2012 on Win 7 64bit.

Best Answer

This is likely because you have a database initializer, that always keep the database in sync with the model, regardless of migrations.

Take a look at the __MigrationHistory and you'll likely see that the schema was updated by the MigrateDatabaseToLatestVersion initializer. The MigrationId will be something like 201307290913337_AutomaticMigration.

To fix it, delete the database, comment out the changes to your model, run Update-Database - now comment in the changes, and add the migration again.

If the last record of the __MigrationHistory table is indeed a xxxxxxx_AutomaticMigration one, you can just delete it and run Add-Migration again, and your migration class should have something useful in its Up and Down methods.

You should make sure that you run Add-Migration when the changes are not already made.