1 00:00:00,250 --> 00:00:01,980 An easy option for applying, 2 00:00:01,980 --> 00:00:06,740 any pending migrations to databases hosted in shared environments is to configure 3 00:00:06,740 --> 00:00:11,370 EF to use the migrate database to latest version, database initializer. 4 00:00:13,385 --> 00:00:18,263 We're currently setting the database initializer to know, in order to disable 5 00:00:18,263 --> 00:00:22,568 database initialization in favor of using the code first migrations, 6 00:00:22,568 --> 00:00:26,340 update database command to update our database. 7 00:00:26,340 --> 00:00:27,140 Let's start. 8 00:00:27,140 --> 00:00:32,500 With adding a new database initializer then, we'll add preprocessor directives so 9 00:00:32,500 --> 00:00:36,820 we can configure a different database initializer per build configuration. 10 00:00:39,145 --> 00:00:42,339 Allow the new database initializer below our current one. 11 00:00:44,004 --> 00:00:46,934 Database.SetInitializer (new 12 00:00:46,934 --> 00:00:52,088 MigrateDatabaseToLatestVersion) Of type 00:00:58,692 In addition to the context class type, the migrate database to latest version, 14 00:00:58,692 --> 00:01:02,267 generic class also requires us to specify the code first 15 00:01:02,267 --> 00:01:07,230 migrations configuration class type, which in our case is Configuration. 16 00:01:10,820 --> 00:01:13,249 Don't forget to add the using directive, 17 00:01:13,249 --> 00:01:16,630 for the ComicBookGalleryModel.Migrations namespace. 18 00:01:21,370 --> 00:01:24,210 Now, let's add our preprocessor directives. 19 00:01:26,070 --> 00:01:31,196 Right before our first database initializer, let's add an if directive 20 00:01:31,196 --> 00:01:35,590 that checks for the presence of the debug symbol, #if DEBUG. 21 00:01:36,690 --> 00:01:40,879 Then, in between the two database initializers, add an else directive. 22 00:01:42,505 --> 00:01:46,510 #else. 23 00:01:46,510 --> 00:01:51,365 And, just after the second database initializer, add an end if directive, 24 00:01:51,365 --> 00:01:54,510 #endif. 25 00:01:54,510 --> 00:01:58,470 Let's test our new database initializer by downgrading our database to 26 00:01:58,470 --> 00:02:03,730 the previous migration in running our app using the release build configuration 27 00:02:03,730 --> 00:02:07,220 to see if the database will be upgraded to the latest migration. 28 00:02:11,142 --> 00:02:15,169 First, let's downgrade our database, 29 00:02:15,169 --> 00:02:21,735 update-database -targetmigration AddBioPropertyToArtist. 30 00:02:22,807 --> 00:02:27,024 Using the SQL Server Object Explorer, 31 00:02:27,024 --> 00:02:32,135 we can verify that the comic book average rating 32 00:02:32,135 --> 00:02:41,080 table that is created by our latest migration isn't in the list of tables. 33 00:02:41,080 --> 00:02:44,594 Now, let's change our build configuration to release and 34 00:02:44,594 --> 00:02:48,395 start the application without debugging by pressing Ctrl F5. 35 00:02:56,254 --> 00:02:58,290 Here's our list of comic books. 36 00:02:58,290 --> 00:03:00,510 Press Enter twice to continue execution. 37 00:03:03,735 --> 00:03:11,686 Refresh the Tables folder, And, 38 00:03:11,686 --> 00:03:15,120 here's our ComicBookAverageRating table. 39 00:03:15,120 --> 00:03:18,020 If we view the data in the migration history table, 40 00:03:18,020 --> 00:03:21,510 we can confirm that all three of our migrations have been applied. 41 00:03:26,586 --> 00:03:30,966 By using the database initializer to migrate the database to the latest 42 00:03:30,966 --> 00:03:34,981 migration is easy to do and works well with automated workflows, 43 00:03:34,981 --> 00:03:37,790 it's not always possible to use. 44 00:03:37,790 --> 00:03:42,810 In some situations, developers don't have direct access to test and 45 00:03:42,810 --> 00:03:47,340 production environment databases or the servers that they're hosted on. 46 00:03:47,340 --> 00:03:52,130 Instead, developers have to coordinate any updates to those databases 47 00:03:52,130 --> 00:03:56,210 through a database administrator or DBA. 48 00:03:56,210 --> 00:03:58,550 Luckily, there's a workaround. 49 00:03:58,550 --> 00:04:03,440 We can use the Update-Database command to generate a SQL script which we can 50 00:04:03,440 --> 00:04:08,310 hand off to our DBA, who can then review and apply it to the database. 51 00:04:09,640 --> 00:04:12,880 To start, let's downgrade our database to the previous migration. 52 00:04:13,990 --> 00:04:18,310 I'll press the up arrow key to recall the previously executed command. 53 00:04:20,360 --> 00:04:24,666 Then, we can run the update database command with the script flag. 54 00:04:24,666 --> 00:04:29,053 update-database -script. 55 00:04:33,027 --> 00:04:37,548 When the command completes, it'll open the generated script into a new tab for 56 00:04:37,548 --> 00:04:38,370 us to review. 57 00:04:43,746 --> 00:04:48,210 Here's to create table SQL statement to create the ComicBookAverageRating table. 58 00:04:49,310 --> 00:04:52,804 Our SQL statement, to migrate the AverageRating data from the comic book 59 00:04:52,804 --> 00:04:55,015 table to the ComicBookAverageRating table. 60 00:05:00,180 --> 00:05:05,205 To ALTER TABLE statements, one to drop the ComicBookAverageRating column and 61 00:05:05,205 --> 00:05:10,457 another to add the comic book ID foreign key to the ComicBookAverageRating table. 62 00:05:13,170 --> 00:05:14,023 Then lastly, 63 00:05:14,023 --> 00:05:19,350 an answer statement to add this migration to the MigrationHistory table. 64 00:05:19,350 --> 00:05:23,000 In addition to generating a script to apply the latest migration to 65 00:05:23,000 --> 00:05:27,110 the database, we can also generate an item potent script 66 00:05:27,110 --> 00:05:31,240 that can upgrade a database currently at any version. 67 00:05:31,240 --> 00:05:34,700 To the latest version with an item potent script, 68 00:05:34,700 --> 00:05:38,610 we can safely executed against any version of the database. 69 00:05:38,610 --> 00:05:42,646 As a contains logic to determine which migrations have been applied and 70 00:05:42,646 --> 00:05:44,390 which haven't. 71 00:05:44,390 --> 00:05:49,595 To generate an item potent script, we specify the source migration 72 00:05:49,595 --> 00:05:54,776 flag along with the dollar sign initial database migration name. 73 00:05:54,776 --> 00:06:00,568 Update database -script 74 00:06:00,568 --> 00:06:10,051 -sourcemigration $initialDatabase. 75 00:06:15,069 --> 00:06:19,230 In the generated script, we can see that it includes a query to get the current 76 00:06:19,230 --> 00:06:21,737 migration from the MigrationHistory table. 77 00:06:25,089 --> 00:06:28,050 And, the conditionals to only apply migrations that 78 00:06:28,050 --> 00:06:30,100 haven't been previously applied. 79 00:06:31,651 --> 00:06:36,694 Regardless of the approach that you end up using to apply migrations to databases 80 00:06:36,694 --> 00:06:41,812 in shared environments, it's important to take the necessary time to review and 81 00:06:41,812 --> 00:06:46,750 test migrations before they are applied to production databases. 82 00:06:46,750 --> 00:06:50,270 Even if you are able to recover from a failed migration, the resulting 83 00:06:50,270 --> 00:06:55,930 application downtime can be costly and a frustrating experience for your users. 84 00:06:55,930 --> 00:06:57,310 Let's recap this section. 85 00:06:58,440 --> 00:07:02,310 We made a change to our model and took a closer look at using the Add-Migration 86 00:07:02,310 --> 00:07:04,670 command to create migrations. 87 00:07:04,670 --> 00:07:09,770 We saw how to use the update-database command to downgrade the database and 88 00:07:09,770 --> 00:07:12,480 an example of modifying a migration. 89 00:07:12,480 --> 00:07:17,050 We discussed workflows and environments, and learned about the deployment options 90 00:07:17,050 --> 00:07:21,689 available to us for applying migrations to databases in shared environments. 91 00:07:22,930 --> 00:07:24,430 Thanks for hanging out with me and 92 00:07:24,430 --> 00:07:27,850 learning about Entity Framework Code First Migrations. 93 00:07:27,850 --> 00:07:30,760 Now, it's a great time to reinforce what you've learned 94 00:07:30,760 --> 00:07:33,950 by using Code First Migrations on a practice project. 95 00:07:33,950 --> 00:07:38,740 For example, you can take the comic book library manager console application 96 00:07:38,740 --> 00:07:43,450 from an earlier Treehouse course on EF and update it to use migrations. 97 00:07:43,450 --> 00:07:47,670 Or, you could design and create your own entity data model and 98 00:07:47,670 --> 00:07:51,380 add migrations to your project as you evolve your model. 99 00:07:51,380 --> 00:07:52,680 If you haven't already, 100 00:07:52,680 --> 00:07:56,176 be sure to check out Treehouse's other courses on entity framework. 101 00:07:56,176 --> 00:08:01,437 See the teachers notes for links to courses that cover the basics of EF and 102 00:08:01,437 --> 00:08:05,080 how to use EF within in ASP.NET MVC application. 103 00:08:05,080 --> 00:08:09,520 There are also other great online and offline EF resources available. 104 00:08:09,520 --> 00:08:13,410 Again, see the teacher's notes for a list of these resources and 105 00:08:13,410 --> 00:08:17,000 don't forget if you ever have a question about EF or 106 00:08:17,000 --> 00:08:20,320 get stuck on something, check out the Treehouse community. 107 00:08:20,320 --> 00:08:22,890 You'll find, there are other students and 108 00:08:22,890 --> 00:08:25,890 a great team of moderators who can help, see you next time.