3

Ok so a bit of background how I got here. I am rebuilding an old MVC3 site. I had a recent Database First MVC5 EF6 project with the same UI package that I wanted to use. I copied the existing project and ripped almost everything out of it (all project specific POCO Models, DbContext, Views etc) basically I just left the new Identity stuff in there along with my UI Framework.

I created scripts to manually convert my database from SQLMembership to Identity 2.0 (and a little bit of code for password conversion). Everything was going fine, it compiled, I could log in as an existing user.

Next step, I created a new temp Project (I didn't want to mess up the actual project namespaces), generated the POCO Models from the new database for all the existing tables, copied them across to my project and have just manually written the DataModel Fluent API code. I had a few validation errors on model generation which I fixed no problem. But now I get an exception error when trying to do anything that would access the DataModel that __MigrationHistory already exists.

Further investigation looking at the EF SQL debug output shows it is trying to run scripts to create all the tables in my database, it finally throws an error when it tries to create the __MigrationHistory table since it already exists (though I should note it has no records).

So what am I doing differently? How does EF know not to try to do this in my other project which works fine, and in terms of DataModel structure and POCO Model structure is the same as my new project? What should I do to fix it? I know I could make my own initialiser (How can I disable the use of the __MigrationHistory table in Entity Framework 4.3 Code First?) but I'd prefer to understand what is going on and fix at the source rather than code around it.

1 Answer 1

5

Remove the existing Migrations folder in your project. DROP the table "__MigrationHistory" from the existing database. Now you can rebuild this history table:

PM> enable-migrations
PM> add-migration Initial -IgnoreChanges
PM> update-database

-IgnoreChanges creates empty Up() methods and it doesn't make any changes to existing objects.

2
  • Thanks VahidN this did work. Some notes; I did not have an original Migrations folder. After running these commands in an attempt to figure out what was going on, I removed the newly created Migrations folder, and also deleted the record created in the new __MigrationsHistory table. So as far as I can tell, everything is back to the same state as before I ran these commands, but now everything works as normal as and as expected. So what else has happened? Any more info or links you can provide so I can understand more would be appreciated. Thanks again for your help.
    – Josh
    Mar 3, 2015 at 21:03
  • 2
    The MigrationHistory table holds the hashes of your domain models and also existing tables in the database. If you change one of your models, EF is able to compute this hash again and compare it with its old value in MigrationHistory table and then update your database again. But if you change the database manually, it will find out something has changed in the database, because the hashes are different, but this time it doesn't know how to resolve this conflict. So in code first approach don't try to behave/design like the database first approach.
    – VahidN
    Mar 4, 2015 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.