Deployment Options8:26 with James Churchill
Regardless of your specific development, testing, and deployment workflow, you'll need a way to apply pending migrations to databases hosted in shared environments. Let's review the deployment options that Code First Migrations makes available to us.
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.4 -b deployment-options
Finished Project Code
Here's how to get the code for the finished project:
git clone firstname.lastname@example.org:treehouse-projects/dotnet-ef-migrations.git cd dotnet-ef-migrations
Additional Project Ideas
Now that you've completed this course, it's a great time to reinforce what you've learned by working on a project.
Idea #1: Update the Comic Book Library Manager Console App
Update the Comic Book Library Manager console app—from the Entity Framework Basics course—to use Code First Migrations.
Here's the link to download the completed Comic Book Library Manager project files.
Idea #2: Design Your Own Entity Data Model
Design your own entity data model, enable migrations, then practice making model changes and adding migrations. You could purposely start with an overly simplistic model, so that you'll have plenty of model changes to make once you've enabled migrations.
Additional Treehouse Entity Framework Courses
Treehouse has two more EF courses (one that's available now and another that'll be available soon).
If you ever have a question about EF or get stuck on something, check out the Treehouse Community. You’ll find there other students and a great team of moderators who can help.
An easy option for applying, 0:00 any pending migrations to databases hosted in shared environments is to configure 0:01 EF to use the migrate database to latest version, database initializer. 0:06 We're currently setting the database initializer to know, in order to disable 0:13 database initialization in favor of using the code first migrations, 0:18 update database command to update our database. 0:22 Let's start. 0:26 With adding a new database initializer then, we'll add preprocessor directives so 0:27 we can configure a different database initializer per build configuration. 0:32 Allow the new database initializer below our current one. 0:39 Database.SetInitializer (new 0:44 MigrateDatabaseToLatestVersion) Of type <Context,. 0:46 In addition to the context class type, the migrate database to latest version, 0:53 generic class also requires us to specify the code first 0:58 migrations configuration class type, which in our case is Configuration. 1:02 Don't forget to add the using directive, 1:10 for the ComicBookGalleryModel.Migrations namespace. 1:13 Now, let's add our preprocessor directives. 1:21 Right before our first database initializer, let's add an if directive 1:26 that checks for the presence of the debug symbol, #if DEBUG. 1:31 Then, in between the two database initializers, add an else directive. 1:36 #else. 1:42 And, just after the second database initializer, add an end if directive, 1:46 #endif. 1:51 Let's test our new database initializer by downgrading our database to 1:54 the previous migration in running our app using the release build configuration 1:58 to see if the database will be upgraded to the latest migration. 2:03 First, let's downgrade our database, 2:11 update-database -targetmigration AddBioPropertyToArtist. 2:15 Using the SQL Server Object Explorer, 2:22 we can verify that the comic book average rating 2:27 table that is created by our latest migration isn't in the list of tables. 2:32 Now, let's change our build configuration to release and 2:41 start the application without debugging by pressing Ctrl F5. 2:44 Here's our list of comic books. 2:56 Press Enter twice to continue execution. 2:58 Refresh the Tables folder, And, 3:03 here's our ComicBookAverageRating table. 3:11 If we view the data in the migration history table, 3:15 we can confirm that all three of our migrations have been applied. 3:18 By using the database initializer to migrate the database to the latest 3:26 migration is easy to do and works well with automated workflows, 3:30 it's not always possible to use. 3:34 In some situations, developers don't have direct access to test and 3:37 production environment databases or the servers that they're hosted on. 3:42 Instead, developers have to coordinate any updates to those databases 3:47 through a database administrator or DBA. 3:52 Luckily, there's a workaround. 3:56 We can use the Update-Database command to generate a SQL script which we can 3:58 hand off to our DBA, who can then review and apply it to the database. 4:03 To start, let's downgrade our database to the previous migration. 4:09 I'll press the up arrow key to recall the previously executed command. 4:13 Then, we can run the update database command with the script flag. 4:20 update-database -script. 4:24 When the command completes, it'll open the generated script into a new tab for 4:33 us to review. 4:37 Here's to create table SQL statement to create the ComicBookAverageRating table. 4:43 Our SQL statement, to migrate the AverageRating data from the comic book 4:49 table to the ComicBookAverageRating table. 4:52 To ALTER TABLE statements, one to drop the ComicBookAverageRating column and 5:00 another to add the comic book ID foreign key to the ComicBookAverageRating table. 5:05 Then lastly, 5:13 an answer statement to add this migration to the MigrationHistory table. 5:14 In addition to generating a script to apply the latest migration to 5:19 the database, we can also generate an item potent script 5:23 that can upgrade a database currently at any version. 5:27 To the latest version with an item potent script, 5:31 we can safely executed against any version of the database. 5:34 As a contains logic to determine which migrations have been applied and 5:38 which haven't. 5:42 To generate an item potent script, we specify the source migration 5:44 flag along with the dollar sign initial database migration name. 5:49 Update database -script 5:54 -sourcemigration $initialDatabase. 6:00 In the generated script, we can see that it includes a query to get the current 6:15 migration from the MigrationHistory table. 6:19 And, the conditionals to only apply migrations that 6:25 haven't been previously applied. 6:28 Regardless of the approach that you end up using to apply migrations to databases 6:31 in shared environments, it's important to take the necessary time to review and 6:36 test migrations before they are applied to production databases. 6:41 Even if you are able to recover from a failed migration, the resulting 6:46 application downtime can be costly and a frustrating experience for your users. 6:50 Let's recap this section. 6:55 We made a change to our model and took a closer look at using the Add-Migration 6:58 command to create migrations. 7:02 We saw how to use the update-database command to downgrade the database and 7:04 an example of modifying a migration. 7:09 We discussed workflows and environments, and learned about the deployment options 7:12 available to us for applying migrations to databases in shared environments. 7:17 Thanks for hanging out with me and 7:22 learning about Entity Framework Code First Migrations. 7:24 Now, it's a great time to reinforce what you've learned 7:27 by using Code First Migrations on a practice project. 7:30 For example, you can take the comic book library manager console application 7:33 from an earlier Treehouse course on EF and update it to use migrations. 7:38 Or, you could design and create your own entity data model and 7:43 add migrations to your project as you evolve your model. 7:47 If you haven't already, 7:51 be sure to check out Treehouse's other courses on entity framework. 7:52 See the teachers notes for links to courses that cover the basics of EF and 7:56 how to use EF within in ASP.NET MVC application. 8:01 There are also other great online and offline EF resources available. 8:05 Again, see the teacher's notes for a list of these resources and 8:09 don't forget if you ever have a question about EF or 8:13 get stuck on something, check out the Treehouse community. 8:17 You'll find, there are other students and 8:20 a great team of moderators who can help, see you next time. 8:22
You need to sign up for Treehouse in order to download course files.Sign up