1 00:00:00,000 --> 00:00:04,948 [MUSIC] 2 00:00:04,948 --> 00:00:10,358 Our entity data model currently only contains a single entity, ComicBook. 3 00:00:10,358 --> 00:00:13,610 While the ComicBook entity is an important part of our model, 4 00:00:13,610 --> 00:00:16,260 it's not enough on its own to fully describe our data. 5 00:00:17,540 --> 00:00:22,100 As we've previously discussed, each ComicBook belongs to a series. 6 00:00:22,100 --> 00:00:27,110 Our ComicBook entity class currently contains a SeriesTitle property that, as 7 00:00:27,110 --> 00:00:31,310 the name suggests, contains the title of the series that the comic book belongs to. 8 00:00:32,430 --> 00:00:34,260 If we want to also have a description for 9 00:00:34,260 --> 00:00:39,830 the series, we can add another property to the ComicBook entity, SeriesDescription. 10 00:00:39,830 --> 00:00:41,100 This would work, but 11 00:00:41,100 --> 00:00:45,230 now the ComicBook entity has two properties related to a series. 12 00:00:45,230 --> 00:00:49,580 And if we have more than one ComicBook object instance that belongs to the same 13 00:00:49,580 --> 00:00:54,630 series, the series title and series description property string values 14 00:00:54,630 --> 00:00:58,140 would be repeated within each ComicBook instance. 15 00:00:58,140 --> 00:01:01,250 To avoid this unnecessary duplication of values, 16 00:01:01,250 --> 00:01:04,120 we can add a Series entity to our model. 17 00:01:04,120 --> 00:01:09,170 The Series entity will have ID, Title and Description properties. 18 00:01:09,170 --> 00:01:12,120 Then we can replace the ComicBook entity series title 19 00:01:12,120 --> 00:01:16,720 into series description properties with a reference to the series entity. 20 00:01:16,720 --> 00:01:21,323 The relationship between a ComicBook and a series is known as a One-to-Many or 21 00:01:21,323 --> 00:01:24,520 Many-to-One, depending on your point of view. 22 00:01:24,520 --> 00:01:30,510 From the series point of view, a series contains one or more, or many comics. 23 00:01:30,510 --> 00:01:35,260 And from the ComicBook's point of view, a ComicBook belongs to a single or 24 00:01:35,260 --> 00:01:37,330 one series. 25 00:01:37,330 --> 00:01:41,530 So from the series point of view, their relationship is One-to-Many. 26 00:01:41,530 --> 00:01:45,220 And from the ComicBook's point of view, it's Many-to-One. 27 00:01:45,220 --> 00:01:47,785 In addition to the Many-to-One relationship, 28 00:01:47,785 --> 00:01:51,710 we can model Many-to-Many and One-to-One relationships. 29 00:01:51,710 --> 00:01:55,280 We'll implement a Many-to-Many relationship later in the section. 30 00:01:55,280 --> 00:01:58,603 For more information on how to model a One-to-One, or 31 00:01:58,603 --> 00:02:02,680 One-to-Zero or One relationship, see the teacher's notes. 32 00:02:03,860 --> 00:02:06,260 Let's add a series entity to our model and 33 00:02:06,260 --> 00:02:09,782 define the relationship between the ComicBook and series entities. 34 00:02:10,850 --> 00:02:15,610 Right click on the Models folder and select Add > Class. 35 00:02:16,760 --> 00:02:19,561 Name the class Series and click Add. 36 00:02:23,914 --> 00:02:26,711 Add the public access modifier and 37 00:02:26,711 --> 00:02:31,160 the following properties, an int Property named Id. 38 00:02:32,180 --> 00:02:35,992 This property will contain a unique value for each series in our system. 39 00:02:38,375 --> 00:02:41,233 A string property named Title. 40 00:02:41,233 --> 00:02:43,852 This is the title of the series. 41 00:02:45,196 --> 00:02:48,186 A string property named Description. 42 00:02:50,019 --> 00:02:52,820 This is a description of the series. 43 00:02:52,820 --> 00:02:55,118 Now that we have the series entity class, 44 00:02:55,118 --> 00:02:58,410 let's update the ComicBook entity class to make use of it. 45 00:03:04,903 --> 00:03:09,900 I'll replace the series title property with the series property of type series. 46 00:03:12,280 --> 00:03:15,140 By adding this property to the ComicBook entity class, 47 00:03:15,140 --> 00:03:21,080 we've defined a Many-to-One relationship between the ComicBook and series entities. 48 00:03:21,080 --> 00:03:26,250 Each ComicBook instance can be associated with exactly one series instance, 49 00:03:26,250 --> 00:03:29,910 but there's no restriction with how many ComicBook instances 50 00:03:29,910 --> 00:03:32,820 a series instance can be associated with. 51 00:03:32,820 --> 00:03:38,560 In this relationship, the series entity is the principal and the ComicBook entity 52 00:03:38,560 --> 00:03:43,665 is the dependent, meaning that a ComicBook is dependent upon a series. 53 00:03:43,665 --> 00:03:48,790 EF refers to the series property as a navigation property, as it allows 54 00:03:48,790 --> 00:03:53,805 us to navigate the relationship from the ComicBook entity to the series entity. 55 00:03:55,945 --> 00:04:00,385 Including an Id property is important to do, so EF will know 56 00:04:00,385 --> 00:04:05,325 that we intend the data for the series entity to be stored in its own table. 57 00:04:05,325 --> 00:04:07,815 If we didn't include the id property, 58 00:04:07,815 --> 00:04:11,440 EF would treat the series class as a complex type. 59 00:04:11,440 --> 00:04:12,276 And its title and 60 00:04:12,276 --> 00:04:16,810 description properties would be added to the ComicBook's database table. 61 00:04:16,810 --> 00:04:21,690 Complex types give you a way with an EF to group related sets of properties 62 00:04:21,690 --> 00:04:25,330 within an entity or across entities. 63 00:04:25,330 --> 00:04:30,070 For more information about EF complex types, see the teacher's notes. 64 00:04:30,070 --> 00:04:34,082 We can also define an navigation property on the series entity, 65 00:04:34,082 --> 00:04:35,950 though doing so is optional. 66 00:04:35,950 --> 00:04:41,727 Since a series can be associated with more than one ComicBook, 67 00:04:41,727 --> 00:04:47,177 we need to define the navigation property as a collection, 68 00:04:47,177 --> 00:04:55,640 public ICollection of type ComicBooks get; set for the property type. 69 00:04:55,640 --> 00:05:00,190 I prefer to use the ICollection generic interface instead of a concrete type like 70 00:05:00,190 --> 00:05:04,880 list of T, in order to expose the functionality that I want to provide 71 00:05:04,880 --> 00:05:09,400 without tying myself to a specific collection implementation. 72 00:05:09,400 --> 00:05:14,029 We also need to initialize the collection in a default constructor. 73 00:05:14,029 --> 00:05:19,979 public Series, then ComicBooks 74 00:05:19,979 --> 00:05:25,079 = new List. 75 00:05:26,754 --> 00:05:31,533 This ensures that the ComicBooks property is ready to be used immediately after 76 00:05:31,533 --> 00:05:34,650 instantiating a series entity object. 77 00:05:34,650 --> 00:05:36,600 Now that we've updated our entities, 78 00:05:36,600 --> 00:05:41,440 we need to update the program classes main method to get our app compiling again. 79 00:05:41,440 --> 00:05:44,110 After the break, we'll make those changes and 80 00:05:44,110 --> 00:05:47,560 review how our entity relationship has affected our database.