1 00:00:00,240 --> 00:00:02,800 The up and down methods in the migration class 2 00:00:02,800 --> 00:00:07,030 use the migration fluent API to update the database. 3 00:00:07,030 --> 00:00:12,150 The migration fluent API is provided by methods in the DB migration base class. 4 00:00:14,651 --> 00:00:16,716 The create table method is used, 5 00:00:16,716 --> 00:00:20,120 as the name suggests to create a table in the database. 6 00:00:22,350 --> 00:00:24,530 The first parameter is the name of the table. 7 00:00:26,820 --> 00:00:30,430 The second parameter is a lambda that defines the table columns. 8 00:00:32,230 --> 00:00:35,730 Here we're defining a column named Id that is of type int. 9 00:00:38,860 --> 00:00:43,750 It's configured to not allow nulls and it's an identity column. 10 00:00:45,950 --> 00:00:49,650 And here we're defining a column named name of type string. 11 00:00:52,858 --> 00:00:58,720 It's also configured to not allowed nulls and has a maximum length of 100. 12 00:00:58,720 --> 00:01:03,270 Because this is a fluent API we can dot off the create table method and 13 00:01:03,270 --> 00:01:08,030 make additional method calls to further configure this table. 14 00:01:08,030 --> 00:01:11,950 This call to the primary key method is configuring the Id column 15 00:01:11,950 --> 00:01:13,630 to be the tables primary key. 16 00:01:14,850 --> 00:01:18,555 Scrolling down a bit we can see the call to the create table method for 17 00:01:18,555 --> 00:01:21,230 creating the comic book artist database table. 18 00:01:21,230 --> 00:01:28,440 Four columns are being defined Id, ComicBookId, ArtistId and RoleId. 19 00:01:28,440 --> 00:01:31,030 This table is an explicit bridge table 20 00:01:31,030 --> 00:01:35,950 that defines a many-to-many relationship between comic books and artists. 21 00:01:35,950 --> 00:01:40,710 This many-to-many relationship allows a comic book to be associated with one or 22 00:01:40,710 --> 00:01:46,350 more artists and an artist can be associated with one or more comic books. 23 00:01:46,350 --> 00:01:50,210 For more information about defining entity relationships in EF, 24 00:01:50,210 --> 00:01:54,070 see the teacher's notes for a link to an earlier Treehouse course that can help. 25 00:01:56,508 --> 00:02:01,400 Here we can see that the ComicBookId, ArtistId and RoleId columns are being 26 00:02:01,400 --> 00:02:05,310 configured as foreign keys, using the foreign key method. 27 00:02:09,045 --> 00:02:12,632 The first parameter is the name of the database table that contains 28 00:02:12,632 --> 00:02:13,730 the principal key. 29 00:02:15,971 --> 00:02:19,830 The second parameter is a lambda to select the ForeignKey column that we're 30 00:02:19,830 --> 00:02:20,617 configuring. 31 00:02:22,613 --> 00:02:26,403 And the third parameter is enabling cascadeDeletes. 32 00:02:26,403 --> 00:02:30,879 Remember, enabling cascadeDeletes will cause rows in this table to be deleted 33 00:02:30,879 --> 00:02:33,570 when an associated principle row was deleted. 34 00:02:35,988 --> 00:02:38,807 The index method will create a database index 35 00:02:38,807 --> 00:02:42,150 on a specified database table column. 36 00:02:42,150 --> 00:02:47,320 Creating indexes can help improve query performance when loading related data or 37 00:02:47,320 --> 00:02:48,540 filtering. 38 00:02:48,540 --> 00:02:52,160 For more information on database indexes, see the teacher's notes. 39 00:02:57,600 --> 00:03:02,329 In the down method, we can see calls to the drop versions of the methods that were 40 00:03:02,329 --> 00:03:03,680 used in the up method. 41 00:03:05,840 --> 00:03:08,450 Notice that items related to the tables 42 00:03:08,450 --> 00:03:12,500 need to be dropped first before the tables, themselves can be dropped. 43 00:03:14,250 --> 00:03:17,180 If you try to drop a table before its foreign keys or 44 00:03:17,180 --> 00:03:19,330 indexes were dropped, you'd get an error. 45 00:03:20,450 --> 00:03:22,770 Since we don't have an existing database, 46 00:03:22,770 --> 00:03:27,020 our entire model is represented here in this migration class. 47 00:03:27,020 --> 00:03:30,560 In a later video, we'll make a change to one of our in a days so 48 00:03:30,560 --> 00:03:35,580 we can see a migration that only includes the changes that we made to our model. 49 00:03:35,580 --> 00:03:38,390 If you find an admission or error in your migration and 50 00:03:38,390 --> 00:03:42,010 need to regenerate it you have a couple of options. 51 00:03:42,010 --> 00:03:46,590 The first option is to rerun the add migration command and add the force flag. 52 00:03:50,101 --> 00:03:55,332 Add-migration space, the name of the migration 53 00:03:55,332 --> 00:04:00,066 which is Initial followed by another space, 54 00:04:00,066 --> 00:04:04,810 -force then press Enter to run the command. 55 00:04:08,600 --> 00:04:12,240 Since we already have the migration file open we'll be prompted to reload it. 56 00:04:12,240 --> 00:04:17,275 The second option is to just delete the migration code file and 57 00:04:17,275 --> 00:04:20,420 rerun the add migration command not the force flag. 58 00:04:24,495 --> 00:04:30,884 Add-migration space Initial. 59 00:04:34,880 --> 00:04:39,613 We can do either of these options as many times as we need to in order to get our 60 00:04:39,613 --> 00:04:41,550 migration right. 61 00:04:41,550 --> 00:04:45,010 If the migration has already been applied to our database only to 62 00:04:45,010 --> 00:04:50,210 downgrade the database first before we can delete and regenerate the migration. 63 00:04:50,210 --> 00:04:52,910 We'll see how to downgrade a database in the next section. 64 00:04:54,170 --> 00:04:58,270 It's a good practice to review migrations before you update your database and 65 00:04:58,270 --> 00:05:01,090 commit them to your source code repository. 66 00:05:01,090 --> 00:05:05,430 If you see an error in a migration class it typically means that you have an issue 67 00:05:05,430 --> 00:05:08,980 in your entity data model that needs to be resolved. 68 00:05:08,980 --> 00:05:13,620 While there will be times when editing the migration class is necessary to do we'll 69 00:05:13,620 --> 00:05:16,640 look at one of those situations in the next section. 70 00:05:16,640 --> 00:05:20,620 You should always try to resolve the issue by updating your model. 71 00:05:20,620 --> 00:05:24,270 That will ensure that your requirements for your applications data 72 00:05:24,270 --> 00:05:28,450 are correctly implemented in both the entity data model and the database. 73 00:05:29,510 --> 00:05:34,470 Most errors can be resolved by fixing simple oversights in your entity classes 74 00:05:34,470 --> 00:05:38,290 like adding a missing navigation or foreign key property. 75 00:05:38,290 --> 00:05:41,790 Some errors might require you to add a data annotation 76 00:05:41,790 --> 00:05:47,110 attribute to an entity class property in order to refine a column's data type. 77 00:05:47,110 --> 00:05:50,740 Or you might need to use the EF fluent API 78 00:05:50,740 --> 00:05:55,560 in your context classes on model creating method to refine an entity relationship. 79 00:05:56,740 --> 00:06:00,060 Now that we have our initial migration added to our project 80 00:06:00,060 --> 00:06:02,220 we need to update our database. 81 00:06:02,220 --> 00:06:04,330 We'll see how to do that in the next video.