The Migration Fluent API6:04 with James Churchill
The `Up` and `Down` methods in the migration class use the Migration Fluent API to update the database. Let's take a look at some of the methods in the DbMigration base class that make up the Migration Fluent API.
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 the-migration-fluent-api
For more information on how to define EF entity relationships, see the following Treehouse course that covers the basics of EF.
See the following article for more information about database indexes.
The up and down methods in the migration class 0:00 use the migration fluent API to update the database. 0:02 The migration fluent API is provided by methods in the DB migration base class. 0:07 The create table method is used, 0:14 as the name suggests to create a table in the database. 0:16 The first parameter is the name of the table. 0:22 The second parameter is a lambda that defines the table columns. 0:26 Here we're defining a column named Id that is of type int. 0:32 It's configured to not allow nulls and it's an identity column. 0:38 And here we're defining a column named name of type string. 0:45 It's also configured to not allowed nulls and has a maximum length of 100. 0:52 Because this is a fluent API we can dot off the create table method and 0:58 make additional method calls to further configure this table. 1:03 This call to the primary key method is configuring the Id column 1:08 to be the tables primary key. 1:11 Scrolling down a bit we can see the call to the create table method for 1:14 creating the comic book artist database table. 1:18 Four columns are being defined Id, ComicBookId, ArtistId and RoleId. 1:21 This table is an explicit bridge table 1:28 that defines a many-to-many relationship between comic books and artists. 1:31 This many-to-many relationship allows a comic book to be associated with one or 1:35 more artists and an artist can be associated with one or more comic books. 1:40 For more information about defining entity relationships in EF, 1:46 see the teacher's notes for a link to an earlier Treehouse course that can help. 1:50 Here we can see that the ComicBookId, ArtistId and RoleId columns are being 1:56 configured as foreign keys, using the foreign key method. 2:01 The first parameter is the name of the database table that contains 2:09 the principal key. 2:12 The second parameter is a lambda to select the ForeignKey column that we're 2:15 configuring. 2:19 And the third parameter is enabling cascadeDeletes. 2:22 Remember, enabling cascadeDeletes will cause rows in this table to be deleted 2:26 when an associated principle row was deleted. 2:30 The index method will create a database index 2:35 on a specified database table column. 2:38 Creating indexes can help improve query performance when loading related data or 2:42 filtering. 2:47 For more information on database indexes, see the teacher's notes. 2:48 In the down method, we can see calls to the drop versions of the methods that were 2:57 used in the up method. 3:02 Notice that items related to the tables 3:05 need to be dropped first before the tables, themselves can be dropped. 3:08 If you try to drop a table before its foreign keys or 3:14 indexes were dropped, you'd get an error. 3:17 Since we don't have an existing database, 3:20 our entire model is represented here in this migration class. 3:22 In a later video, we'll make a change to one of our in a days so 3:27 we can see a migration that only includes the changes that we made to our model. 3:30 If you find an admission or error in your migration and 3:35 need to regenerate it you have a couple of options. 3:38 The first option is to rerun the add migration command and add the force flag. 3:42 Add-migration space, the name of the migration 3:50 which is Initial followed by another space, 3:55 -force then press Enter to run the command. 4:00 Since we already have the migration file open we'll be prompted to reload it. 4:08 The second option is to just delete the migration code file and 4:12 rerun the add migration command not the force flag. 4:17 Add-migration space Initial. 4:24 We can do either of these options as many times as we need to in order to get our 4:34 migration right. 4:39 If the migration has already been applied to our database only to 4:41 downgrade the database first before we can delete and regenerate the migration. 4:45 We'll see how to downgrade a database in the next section. 4:50 It's a good practice to review migrations before you update your database and 4:54 commit them to your source code repository. 4:58 If you see an error in a migration class it typically means that you have an issue 5:01 in your entity data model that needs to be resolved. 5:05 While there will be times when editing the migration class is necessary to do we'll 5:08 look at one of those situations in the next section. 5:13 You should always try to resolve the issue by updating your model. 5:16 That will ensure that your requirements for your applications data 5:20 are correctly implemented in both the entity data model and the database. 5:24 Most errors can be resolved by fixing simple oversights in your entity classes 5:29 like adding a missing navigation or foreign key property. 5:34 Some errors might require you to add a data annotation 5:38 attribute to an entity class property in order to refine a column's data type. 5:41 Or you might need to use the EF fluent API 5:47 in your context classes on model creating method to refine an entity relationship. 5:50 Now that we have our initial migration added to our project 5:56 we need to update our database. 6:00 We'll see how to do that in the next video. 6:02
You need to sign up for Treehouse in order to download course files.Sign up