locked
Alpha 3 Code First Migrations w/Existing database RRS feed

  • Question

  • I am doing some testing w/the Alpha 3 Code First Migrations, and everything seems to work great if the Update-Database command is used to actually create the database from the models.  What I am trying to figure out is how to update a pre-existing database.  It would seem that there should be way to say "go get the current state" and then apply migrations from that point on. 

    Any insight on this?
    Thanks,
    Bob

    Thursday, November 10, 2011 5:54 PM

Answers

  • Hi Bob,

    Thanks for your feedback.

    Yes, it can run on an existing database and I have tested it.

    1. Set up Code First to mapping to the existing database.

    2. Install "install-package entityframework.migrations"

    3. "add-migration"-->input the migration name

    ---------------------------set the original migration--------

    4. you can run update command on my first post.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Alan_chen Wednesday, November 16, 2011 7:52 AM
    • Marked as answer by Alan_chen Monday, November 21, 2011 6:49 AM
    Monday, November 14, 2011 6:12 AM
  • Thanks Alan - I see the disconnect now -you are correct, this works in the "No Magic" world, which is preferable.  I had set up a spike and was trying to use the "with Magic" version, and that wasn't going so well.

    Bob

    • Proposed as answer by Alan_chen Wednesday, November 16, 2011 7:52 AM
    • Marked as answer by Alan_chen Monday, November 21, 2011 6:49 AM
    Tuesday, November 15, 2011 2:27 AM

All replies

  • Hi Bob,

    Welcome!

    From the Blog here: http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-released.aspx

    -----

     Upgrade/Downgrade to any migration is now available via the –TargetMigration switch.

    Migrate to a Specific Version (Including Downgrade)

    So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.

    1. Let’s say we want to migrate our database to the state it was in after running our ‘MyFirstMigration’ migration. We can use the TargetMigration switch to downgrade to this migration.
      • Run the ‘Update-Database –TargetMigration:"MyFirstMigration"’ command in Package Manager Console

    This command will run the Down script for our ‘AddBlogAbstract’ and ‘MySecondSetOfChanges’ migrations. If you want to roll all the way back to an empty database then you can use the Update-Database –TargetMigration:"0" command.

    -----

    I'm not sure if this is what you want. Please feel free to let me know your feedback.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, November 11, 2011 6:54 AM
  • Thanks Alan, when I try to run this on a pre-existing database that has never had a migration run before, I get an error:   Invalid object name 'dbo.__MigrationHistory'.

    It would be nice if you could run this on an existing database to get an initial migration state (which is what I am trying to do)?

    Bob

    Monday, November 14, 2011 3:50 AM
  • Hi Bob,

    Thanks for your feedback.

    Yes, it can run on an existing database and I have tested it.

    1. Set up Code First to mapping to the existing database.

    2. Install "install-package entityframework.migrations"

    3. "add-migration"-->input the migration name

    ---------------------------set the original migration--------

    4. you can run update command on my first post.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Alan_chen Wednesday, November 16, 2011 7:52 AM
    • Marked as answer by Alan_chen Monday, November 21, 2011 6:49 AM
    Monday, November 14, 2011 6:12 AM
  • Thanks Alan - I see the disconnect now -you are correct, this works in the "No Magic" world, which is preferable.  I had set up a spike and was trying to use the "with Magic" version, and that wasn't going so well.

    Bob

    • Proposed as answer by Alan_chen Wednesday, November 16, 2011 7:52 AM
    • Marked as answer by Alan_chen Monday, November 21, 2011 6:49 AM
    Tuesday, November 15, 2011 2:27 AM
  • Hi Bob,

    You should input name when we add migration to our project.

    ------------------------------------------------

    Run the ‘Update-Database –TargetMigration:"MyFirstCodeMigration" –Force’ command in

    -----------------------------------------------

    http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-with-magic-walkthrough-automatic-migrations.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 16, 2011 7:52 AM
  • Check this out - EntityFramework.SchemaCompare it's a library similar to EF.Migrations allowing to physically compare db schema with underlying model during db initialization or on request.

    https://github.com/kriasoft/data


    http://www.linkedin.com/in/koistya
    Thursday, December 15, 2011 2:10 PM