Adding Our First Migration4:05 with James Churchill
Now that we've enabled migrations in our project, let's add our first or initial migration.
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.3 -b adding-our-first-migration
Migration EDM Snapshots
Migration entity data model snapshots are stored as base-64 strings containing gzipped XML data. This is a compressed version of the EDMX for the model that was used to create the migration. This value is similar to the model values that are stored in the "__MigrationHistory" table.
Now that we've enabled migrations in our project, let's add our first or 0:00 initial migration. 0:04 If it's not already open, go ahead and open the NuGet package manager console. 0:05 From the top menu its Tools > NuGet Package Manager > Package Manager Console. 0:10 To add a migration, type add migration followed by the name we want to use for 0:17 the migration. 0:22 Add-Migration space Initial. 0:23 I like to name my first migration initial but this isn't a requirement. 0:30 You can use whatever name you'd like to use. 0:34 Press enter to run the command. 0:37 Once the command is completed, 0:40 we can see that EF added a file to the migrations folder named initial. 0:42 Prefixed with a time stamp value representing the date and 0:47 time when we created the migration. 0:51 The time stamp is in coordinated universal time or UTC. 0:53 UTC is the primary time standard by which the world regulates clocks and times. 0:58 See the teacher's notes for more information. 1:04 Prefix in the migrations file name with a time stamp 1:07 ensures that our migration files will display in chronological order. 1:09 That makes it easy to identify the order that they were created in. 1:14 When the command completed, it displayed a message in the package manager console 1:18 highlighted here in yellow. 1:22 The designer code for 1:25 this migration file includes a snapshot of your current code first model. 1:26 This snapshot is used to calculate the changes to your model 1:31 when you scaffold the next migration. 1:34 Designer code is code that is generated by Visual Studio or 1:36 other tools running inside a Visual Studio like code first migrations 1:40 to help support functionality within those tools. 1:45 Designer code files ends with the suffix dot designer. 1:49 Visual Studio recognizes this naming convention and 1:53 will nest these files underneath the associated code file. 1:56 The snapshot of our current model isn't located within the code file but 2:00 rather in associated resource file. 2:03 Resource files contain application data stored as name value pairs. 2:06 If we double click the file, Visual Studio will display the resource data in a grid. 2:11 The Target value is a snapshot of the entity data model 2:17 that was used to create the migration. 2:20 The decider code file and 2:22 the associated resource file, is a code first migrations implementation detail. 2:23 And not something that you typically find useful to review. 2:28 Now, let's take a look at the migration itself. 2:36 Notice that the class name is just Initial, 2:51 without the time stamp prefix that's included in the code filename. 2:53 This is one of the rare times where the standard convention of the code filename 2:58 matching the class name is not followed. 3:02 The partial keyword indicates that the Initial class is a partial class. 3:06 Partial classes, allow you to split the definition for a class across one or 3:11 more files. 3:16 The other part of our Initial class is located within the designer code file. 3:17 Our class inherits from the code first migrations DbMigration base class and 3:25 overrides the up and down methods. 3:31 The Up method contains code to update the database to 3:38 be in sync with the model that was used to generate this migration. 3:41 You can think of this method as being responsible for upgrading the database. 3:44 The Down method contains code to remove the updates that were applied 3:51 in the Up method. 3:55 So that the database will be in sync with the previous version of the model. 3:56 You can think of this method as being responsible for downgrading the database. 4:01
You need to sign up for Treehouse in order to download course files.Sign up