Search code examples
entity-frameworkentity-framework-migrations

EF Data migrations won't detect changes when adding new migration


I am using Entity Framework 5.0 Data migrations along with code first. When i add a new field to my model and execute the following command in the package manager console.

 "Add-migration AddedField"

All I get is an empty migration called "n_AddedField", the up and down methods contain no logic.

I tried a bunch of things, reinstalling the EF nuget package, cleaning my solution, rebuilding, manually removing all generated files and directories.

Then i decided that i would scrap all my migrations and start over, and then it got weird. After deleting all my migrations, and the migrationhistory table in the database, i recreated the database using the CreateDatabaseIfNotExists initializer. After doing this, I should be able to create a new initial migration. But when i try to create create a new migration, I get an error saying that there are pending migrations, and lists all the migrations that i just deleted from my project.

I have no idea why and how EF still has any recollection of those migrations. I even tried searching through filecontents looking if the migrations were saved somewhere else or something. But nothing..

Data migrations look really neat when scott hansleman demo's it on stage, but for real work, I'm starting to look for alternatives.

When the project started, we were using EF 4.x and a while back switcted to 5.0, but since the switch i have added a bunch of migrations successfully.

Does anyone have any idea how to solve this problem? Basically i just want to be able to add migrations, and generate a sql script with the changes.


Solution

  • I had a similar problem where a new migration was not being found, and so update-database was giving me the following error no matter what I did:

    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.
    You can use the Add-Migration command to write the pending model changes to a code-based migration.
    

    Doing a "batch clean" solved my problem, suggesting EF was using an old/invalid assembly from a folder other than the currently selected 'solution configuration (e.g. DEBUG)'.

    Hope this helps someone else out there.