Updating the Database4:13 with James Churchill
Now that we have our first migration created, let's see how to update our 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/v1.5 -b updating-the-database
See the following article for an example of how to decode the __MigrationHistory table's "Model" column.
Now we're ready to update our database. 0:00 Well, since we don't have an existing database, 0:02 updating our database will cause code first migrations to create it. 0:05 To update our database, we'll open the Package Manager Console and 0:10 run the update database command. 0:14 Update-database and press Enter. 0:20 Okay, the command completed successfully. 0:25 If we open the SQL Server Object Explorer window, 0:29 we can see our new ComicBookGalleryMigrations database. 0:32 The name in the database is being set using the database connection string 0:40 defined in the project's app.config file. 0:44 We can also expand the Tables folder to see the list of our tables. 0:48 We can use the verbose flag to see the SQL statements that code first migrations is 0:57 running to create our database. 1:02 To test this, let's first delete our database. 1:04 Then rerun the update database command with the verbose flag. 1:10 Update-database -verbose. 1:14 And press Enter. 1:20 Once the command has completed, let's scroll up through the console output. 1:25 Okay, here's where our migration starts. 1:43 Here are the CREATE TABLE SQL statements for the Artist and ComicBookArtist tables. 1:52 Followed by the CREATE INDEX SQL statements to create the indexes on 1:58 the ComicBookArtist table. 2:02 And here are the statements for 2:04 the ComicBook series and Role tables. 2:07 Once all of the tables were created, 2:12 then the ALTER TABLE SQL statements to create the foreign keys were ran. 2:14 The last of the statements that were ran were to create the MigrationHistory 2:20 table and insert a row into that table for this migration. 2:26 The MigrationHistory table is used to track which migrations have 2:35 been applied to the database. 2:38 When you run the update database command, any migrations contained in your project 2:41 that don't have an associated row in this table will get applied to the database. 2:45 The Model column contains a serialized version of the EDMX for 2:52 your entity data model. 2:55 That's what this long string of letters and numbers represents. 2:57 As an aside, 3:08 it's possible to decode the Model column value in order to see the EDMX. 3:08 See the teacher's notes for more information. 3:14 Now that you've seen how to use the update database command to update your local 3:16 database, you might be wondering how to do this in other environments, 3:20 like on a test or production server. 3:24 We'll take a look at deployment options in the next section. 3:26 Let's run our application and make sure that things work as expected. 3:30 Open the Program.cs file, set a break point on the query that 3:36 retrieves the list of comic books and run the application. 3:41 Okay, we'll add our break point. 3:49 Press F10 to execute the query and continue to the next statement. 3:51 If we hover over the ComicBooks variable, 3:57 we can see that the collections count is zero. 3:59 Well, this makes sense. 4:03 While we created our database, our migration didn't include any C data. 4:05 Let's see how to fix that next. 4:11
You need to sign up for Treehouse in order to download course files.Sign up