Downgrading the Database5:01 with James Churchill
Let's see how we can downgrade a database just as easily as we can upgrade a database.
To follow along commiting your changes to this course, you'll need to fork the dotnet-ef-migrations repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-ef-migrations git checkout tags/v2.2 -b downgrading-the-database
Here's a link to a detailed breakdown of the available Code First Migrations commands (including the flags available for each).
So far in this course, we've only been upgrading our database by applying 0:00 the latest pending migration using the update base command. 0:04 But we can dowgrade the database just as easily as we can upgrade the database. 0:08 Before we downgrade our database, 0:12 let's view the data in the migration history table. 0:14 The table contains two rows, one row for each of the migrations that we've applied. 0:24 The migration ID column values match our migration code filenames, 0:32 so the migrations are easy to identify. 0:36 Let's use the update database command to downgrade our 0:45 database to the initial migration, update-database. 0:49 The target migration flag is used to specify the name of the migration 0:54 that we want to update the database to. 0:58 Space, -targetmigration, 1:00 space, initial. 1:06 We're supplying the name initial, 1:09 which is the migration that precedes our latest migration. 1:11 Add bio property to artist. 1:14 This will cause the updated database command to call the down method 1:17 on the ad buy a property to artist migration in order to remove the changes 1:21 that it made to the database, as we downgrade to the initial migration. 1:25 Press enter to run the command. 1:30 If we view the migration history table data again 1:33 We'll see that it only contains a single row, the row for our initial migration. 1:42 We can confirm that the ad file property to artist migration was removed by 1:49 reviewing the artist table columns. 1:54 We can see here that the table currently only contains ID and name columns. 1:58 When you downgrade your database, 2:04 your application can end up in an unusable state. 2:06 Our artist entity class still has the Bio property defined, but 2:11 as we saw just a bit ago, our artist table doesn't contain that column anymore. 2:15 If we run our application, it'll throw an exception. 2:20 Here's the exception, view the detail for 2:24 the exception and expand the exception details. 2:27 The inner exception has a message of invalid column name bio. 2:34 If we were to remove or comment out the bio property and all of the references to 2:43 it in run our application again everything would work fine. 2:47 What if we want to remove all of the applied migrations 2:52 in order to restore our database to its original initial state? 2:55 Code first migrations gives us a special migration name that we can use for 3:00 this explicit purpose. 3:04 Update-database -targetmigration. 3:14 Space $InitialDatabase. 3:21 Now, if we refresh our databases table list in the SQL server object explorer 3:34 window, we can see that all of the tables have been removed. 3:39 We can run the update-database command again without any flags 3:44 to apply all of our project's migrations. 3:48 update-database. 3:50 Refresh the table list And all of our tables are back again. 3:59 View the migration history table data. 4:07 And now it contains two rows again, one row for each of our migrations. 4:14 If it can leave your application in an unusable state, 4:23 why is being able to downgrade your database interesting? 4:26 It's useful when you need to remove the migration that you just applied, so 4:30 you can make a change or correct a problem and sometimes it's useful to restore your 4:34 database to its original initial state or to a specific migration. 4:39 So, you can test the plying of your migrations from that point forward. 4:44 In the next video, we'll take a look at an example of a model change 4:49 that will require us to modify the generated migration in order for 4:52 it to successfully upgrade and downgrade the database. 4:56
You need to sign up for Treehouse in order to download course files.Sign up