locked
MigrateDatabaseToLatestVersion how it works RRS feed

  • Question

  • I started playing with EF and CF. I went through some demos and it works fine with Addmigrations and update migrations.

    I understand that "MigrateDatabaseToLatestVersion" allows automatically upgrading on application startup.

    By "application startup", does this mean that if somebody has accidently deleted a column from one of the tables will be added again on start up.

    I explicitly deleted a column but when I run the program I get error.

    So my question is  : Does CF and EF automatic upgrade works/considers only for new code or columns done inside the project or it checks the state of the database and if it not similar to the model in the CF project then it will do the needful?

    Thanks 

    Saturday, March 16, 2013 11:29 AM

Answers

  • Ideally you should not be deleting the columns manually from the tables when you are using a code first approach. When you delete columns from DB , CF wont understand the missing column, since CF will check the mapping table which lies within your DB. If you remove the MigrationId from [dbo].[__MigrationHistory] then next time your DB will upgrade with the new version on the model after ensuring that this will make you DB consistent. But the best way is to use the Migration.exe to Add or remove the migrations from the DB and updgrade migrations manually or automate it.

    Cheers
    • Edited by Sandeep Pote Monday, April 15, 2013 9:57 PM
    • Marked as answer by eliassal Thursday, March 9, 2017 2:17 PM
    Monday, April 15, 2013 9:54 PM

All replies

  • Hi,

    I think this article helps: http://msdn.microsoft.com/en-us/data/jj591621.aspx


    Monday, March 18, 2013 6:13 AM
  • It is this article I started with but this one does not answer my question :  Does CF and EF automatic upgrade works/considers only for new code or columns done inside the project or it checks the state of the database and if it not similar to the model in the CF project then it will do the needful?
    Monday, March 18, 2013 10:46 AM
  • Ideally you should not be deleting the columns manually from the tables when you are using a code first approach. When you delete columns from DB , CF wont understand the missing column, since CF will check the mapping table which lies within your DB. If you remove the MigrationId from [dbo].[__MigrationHistory] then next time your DB will upgrade with the new version on the model after ensuring that this will make you DB consistent. But the best way is to use the Migration.exe to Add or remove the migrations from the DB and updgrade migrations manually or automate it.

    Cheers
    • Edited by Sandeep Pote Monday, April 15, 2013 9:57 PM
    • Marked as answer by eliassal Thursday, March 9, 2017 2:17 PM
    Monday, April 15, 2013 9:54 PM
  • I know its an old post but ...

    The automatic update on startup will only be performed when there is a pending migration in your code base!

    CF does version checks against the __MigrationHistory table of your DB it doesn't compare the DB scheme with your code! As 'Sandeep Pote' mentioned you could manualy delete the entry of the last migration from the __MigrationHistory table but that could mess things up.

    The best way to remove the column is to remove it from your model, add a new migration and publish the new version.

    Tuesday, January 5, 2016 3:17 PM
  • thanks sandeep
    Thursday, March 9, 2017 11:48 AM