Enabling Migrations6:06 with James Churchill
Let's do a quick review of the project that we'll be working with in this course. And let's see how to enable migrations for our project.
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.2 -b enabling-migrations
You can download the project files for this course using this link.
NuGet Package Manager Console
In addition to running commands that are included with the NuGet packages that we have installed in our project, the Visual Studio Package Manager Console also gives us a way to interact with NuGet from a command line. We can search for packages, install and uninstall packages, and more.
You can use the
Find-Package command to search for packages.
This will list all of the available NuGet packages that contain "elmah" in their package Id. Elmah is a commonly used logging library (see http://elmah.github.io/).
To install a package, you can use the
Uninstall-Package command allows you to uninstall a package.
For more information about package management using NuGet, see the following Treehouse workshop.
For more information about the NuGet Package Manager Console or PowerShell, see the following resources.
As we've learned about Code First Migrations, we'll be working with a model 0:00 borrowed from a comic gallery application that allows users to search and 0:04 view information about comic books. 0:08 If you're following along, see the teacher's notes for 0:11 a link to download the project files. 0:13 I've already downloaded the project files and open the solution in Visual Studio. 0:16 In order to keep the focus on Code First Migrations, this project 0:20 is a simple console application that just contains the entity and 0:24 context classes from the original comic book gallery application. 0:28 If we open the packages.config file, we can see that NuGet has been used to 0:32 add the entity framework package to this project. 0:37 In the references folder, 0:40 here's the reference to the entity framework assembly. 0:41 See the teacher's notes for a link to a Treehouse workshop that would teach you 0:45 more about how to use the NuGet Package Manager. 0:50 In the data folder, as our database context class, 0:55 which is simply named Context. 0:58 It contains four DbSet properties, 1:00 ComicBooks, Series, Artists, and Roles. 1:06 An example of a series would be the Amazing Spider-Man or Batman. 1:12 A comic book belongs to a series, 1:14 and has an issue number. 1:19 For example, issue number one of the Amazing Spider-Man series. 1:23 A comic book can be associated with one or more artists. 1:27 And each artist is associated with a role for that comic book. 1:30 For example, the artist, Stan Lee, can be assigned the role of script for 1:34 issue number one of the Amazing Spider-Man series. 1:39 Notice that we're setting the database initializer to null, 1:44 which disables database initialization within EF. 1:48 Without this, if we ran our application, EF would create our database. 1:51 We don't want that to happen. 1:57 We're going to use Code First Migrations to create our database. 1:58 Later in this course, we'll see how we can configure ES database initializer 2:02 to always update the database, to the latest migration in our project. 2:07 The Models folder contains all of our entity classes. 2:11 We have five entity classes, Artist, ComicBook, 2:16 ComicBookArtist, Role, and Series. 2:21 If you're following along, feel free to pause the video and explore these classes. 2:27 In the program.cs file, 2:32 the static main method is the entry point into our console application. 2:35 It's the method that the .NET runtime calls when we run our application. 2:40 It instantiates an instance of the context class, 2:45 queries a list of the available comic books. 2:49 Loops through that list, and outputs the information for 2:55 each comicBook to the console. 2:59 If we were to run the app, we'd get an exception, 3:01 because we don't have a database created yet for our application. 3:04 By the end of the section, we've created our database from our initial migration, 3:09 and populated the database with some C data. 3:13 To use Code First Migrations, 3:15 we need to start with enabling migrations for our project. 3:17 We do that by running a command in the NuGet Package Manager Console. 3:21 Let's open the Package Manager Console. 3:27 From the top menu, it's Tools, NuGet Package Manager, Package Manager Console. 3:29 The Package Manager Console gives us a way within Visual Studio to interact 3:36 with NuGet from a command line. 3:41 We can search for packages, install and uninstall packages, and more. 3:43 We can also run commands that are included with the packages that 3:49 we have installed in our project. 3:52 The entity framework, 3:54 NuGet Package includes a set of commands related to Code First Migrations. 3:55 We can use those commands to enable migrations, add a migration, 4:00 and update our database. 4:05 To enable migrations for our project, let's run the enable migrations command. 4:07 Enable-Migrations, or 4:12 simply enable-migrations. 4:16 All in lowercase letters. 4:22 Proper capitalization isn't necessary as these commands aren't case sensitive. 4:24 Press Enter to run the command. 4:29 EF will add a migrations folder to our project along with 4:32 the configuration .cs file that defines a configuration class. 4:36 We'll take a look at that class in the next video. 4:41 The enable migrations command, check if the context targets an existing database. 4:44 If an existing database is found, a migration named _initialcreate 4:50 that represents the starting point of the database will be added. 4:55 If there isn't an existing database, 4:59 then you'll need to explicitly create the initial migration and 5:01 update your database, which is what we'll do later in the section. 5:05 It's worth noting that the enable migrations command won't work if your 5:08 project doesn't contain a class that inherits from the EF DB context base 5:13 class. 5:17 You'll get an error message that effect. 5:17 Enabling migrations only needs to be done once. 5:21 If we run the enable migrations command a second time, we'll get an error message. 5:24 Migrations have already been enabled in project, ComicBookGalleryModel. 5:30 It's typical to wait to enable migrations 5:36 until you're ready to deploy your first production release of your application. 5:38 Prior to that, it's usually more efficient to let EF drop and 5:43 create your database when there are model changes. 5:47 Also, even if you think your model won't change, 5:50 once you've gone into production, it's still best to switch your project 5:53 over to using Code First Migrations as you go into production. 5:57 That way, you'll be ready to make model changes when the time comes. 6:01
You need to sign up for Treehouse in order to download course files.Sign up