Code First Migrations RRS feed

  • Question

  • Hi there,

    I have a project that uses the entity framework and I have code first migrations enabled.  When I modify the models I run the update-database command and the server is synchronised.

    That's all very good for my development server, but what about instances I already have deployed?  Is there a way to get this to run automatic upgrading on my deployed environments without using visual studio package manager console?


    Somewhere out there...

    Tuesday, February 25, 2014 1:37 PM


  • Hi,

    What we do is to generate the sql scripts that are needed to get the database synchronized to the model. :

    Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: AddPostAbstract

    We keep the scripts in source control and run them when we release to the next environment. We make a backup of the database before a release so we don't need to use/create rollback scripts. In case of a tollback we restore the backup.

    Sometimes tables also need data with a release, we create scripts for this also and run them together with the migration scripts.

    If needed all of this can be automated using the same scripts.

    If you have an already deployed database my best guess is that you get a backup of that database, connect from your application to this database and create the scripts from that using the ef powertools (I have not done this myself but I can imagine that this would work), this post should get you started : http://lancelarsen.com/entity-framework-code-first-existing-database-102/ I believe that in the latest version of EF this should be easier (without the power tools) I remember reading about it but can't find the post.

    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    • Edited by KeesDijk Tuesday, February 25, 2014 9:07 PM
    • Marked as answer by Fred Bao Thursday, March 6, 2014 7:36 AM
    Tuesday, February 25, 2014 8:43 PM