1 00:00:00,240 --> 00:00:04,190 The comic book to series relationship is not the only relationship that 2 00:00:04,190 --> 00:00:06,420 we need to define in our model. 3 00:00:06,420 --> 00:00:09,250 Each comic book could also be associated with one or 4 00:00:09,250 --> 00:00:14,770 more artists and each artist can be associated with one or more comic books. 5 00:00:14,770 --> 00:00:18,350 This kind of relationship is known as a many to many relationship. 6 00:00:20,240 --> 00:00:22,650 Let's add an artist entity to our model and 7 00:00:22,650 --> 00:00:25,980 define the relationship between the comic book and artist entities. 8 00:00:27,040 --> 00:00:31,330 Right click on the model's folder and select Add Class. 9 00:00:32,670 --> 00:00:36,527 Name the class, artist and click Add. 10 00:00:41,516 --> 00:00:45,594 Add the Public Access modifier and the following properties. 11 00:00:47,490 --> 00:00:49,430 An int property named Id. 12 00:00:51,210 --> 00:00:55,202 Like our other entities, this property will contain a unique value for 13 00:00:55,202 --> 00:00:56,743 each artist in our system. 14 00:01:00,675 --> 00:01:03,620 A string property named Name. 15 00:01:05,250 --> 00:01:07,410 This is the artist's name. 16 00:01:07,410 --> 00:01:10,002 Now that we have the artist class, 17 00:01:10,002 --> 00:01:15,262 let's define the many to many relationship between the comic book and artist Indies. 18 00:01:15,262 --> 00:01:20,020 While we're here we can start by adding a collection of comic books to our 19 00:01:20,020 --> 00:01:21,600 artist class. 20 00:01:21,600 --> 00:01:25,344 Public, Icollection of type comic book, 21 00:01:25,344 --> 00:01:29,816 comic books and we'll add a default constructor so 22 00:01:29,816 --> 00:01:35,225 that we can initialize the comic books collection property. 23 00:01:38,083 --> 00:01:43,435 Public Artist ComicBooks = 24 00:01:43,435 --> 00:01:48,789 new List of . 25 00:01:53,620 --> 00:01:58,378 Now let's add a collection of artists 26 00:01:58,378 --> 00:02:02,400 to the comic book entity class. 27 00:02:02,400 --> 00:02:09,575 Public I collection of type artist ,artists and 28 00:02:09,575 --> 00:02:15,000 a default constructor to initialize 29 00:02:15,000 --> 00:02:20,775 the artist collection property public 30 00:02:20,775 --> 00:02:26,910 comic book artists = new list of artist. 31 00:02:26,910 --> 00:02:30,740 By adding the comic books navigation collection property to the artist entity 32 00:02:30,740 --> 00:02:35,940 class in the artist navigation collection property to the comic book entity class, 33 00:02:35,940 --> 00:02:40,340 we've defined a many to many relationship between the comic book and 34 00:02:40,340 --> 00:02:42,070 artist entities. 35 00:02:42,070 --> 00:02:46,930 Each comic book instance can be associated with one or more artist instances. 36 00:02:46,930 --> 00:02:52,290 And each artist instance can be associated with one or more comic book instances. 37 00:02:52,290 --> 00:02:55,870 With our new artist entity and artist to comic books many to many 38 00:02:55,870 --> 00:03:01,660 relationship in place, we can update our test data to make use of these changes. 39 00:03:01,660 --> 00:03:04,160 Let's start with adding a second series. 40 00:03:04,160 --> 00:03:07,064 I'll copy and paste this code, 41 00:03:07,064 --> 00:03:12,339 change the series variables to series one and series two, 42 00:03:12,339 --> 00:03:18,268 and set the title of the second series to The Invincible Iron Man. 43 00:03:25,500 --> 00:03:31,080 Then let's define some artist's, 44 00:03:31,080 --> 00:03:38,100 var artist1 = new Artist, name = Stan Lee. 45 00:03:42,160 --> 00:03:48,608 Var artist2 = new artist, 46 00:03:48,608 --> 00:03:53,449 name = Steve Ditko. 47 00:03:55,972 --> 00:04:00,357 And var artist 3, 48 00:04:00,357 --> 00:04:03,920 = new artist, 49 00:04:03,920 --> 00:04:08,584 name = Jack Kirby. 50 00:04:10,080 --> 00:04:14,090 We need to assign our comic book entity objects to their own variables so 51 00:04:14,090 --> 00:04:18,790 we can call the add method on the comic book's artist collection property. 52 00:04:18,790 --> 00:04:22,358 Let's cut this comic book and the object instantiation code, 53 00:04:26,160 --> 00:04:31,533 And paste it to the right of var comicBook 1. 54 00:04:31,533 --> 00:04:35,201 Remember that we renamed a series variable to series 1. 55 00:04:35,201 --> 00:04:39,229 So let's replace the reference to the old variable with a new variable. 56 00:04:42,338 --> 00:04:48,696 Now let's add our first two artists, Stan Lee and Steve Ditko, 57 00:04:48,696 --> 00:04:55,316 to this comic book comicBook1.Artists.Add(artist1);. 58 00:04:55,316 --> 00:05:02,722 And comicBook1.Artists.Add(artist2);. 59 00:05:02,722 --> 00:05:05,572 Now repeat that process for the second comic book. 60 00:05:08,700 --> 00:05:15,113 Cut the comic book entity object instantiation code and 61 00:05:15,113 --> 00:05:19,888 paste it to the right of var comicBook2, 62 00:05:19,888 --> 00:05:25,074 rename the series variable to series1, and 63 00:05:25,074 --> 00:05:30,267 add our first two artists to this comic book. 64 00:05:40,312 --> 00:05:45,543 While we're at it let's add a third comic book, 65 00:05:45,543 --> 00:05:50,290 var comicBook3 = new ComicBook. 66 00:05:50,290 --> 00:05:53,293 I'll set the series property to our second series. 67 00:05:56,830 --> 00:06:04,027 The issue number to one and publishedOn to DateTime.Today. 68 00:06:04,027 --> 00:06:07,663 For this comic book let's add our first and third artists. 69 00:06:18,060 --> 00:06:22,779 Let's fix our comic books db set property add method calls by passing in 70 00:06:22,779 --> 00:06:25,861 the variables comicBook1 and comicBook2. 71 00:06:29,102 --> 00:06:31,840 And don't forget that we added a third comic book. 72 00:06:31,840 --> 00:06:38,220 So, we need to add a third call to the comic books db set add method. 73 00:06:38,220 --> 00:06:43,700 Context.ComicBooks.add(comicBooks3). 74 00:06:43,700 --> 00:06:45,840 Now that we've updated our test data. 75 00:06:45,840 --> 00:06:49,880 Let's update our comic books query to include our artist data. 76 00:06:49,880 --> 00:06:54,480 To do that we need to use an include method call, just like we did when 77 00:06:54,480 --> 00:07:01,582 including the series data .include cb goes to cb.artists. 78 00:07:03,400 --> 00:07:07,930 Let's update our output to include a list of each comic book's artist. 79 00:07:07,930 --> 00:07:13,967 I'll use a link query to transform the collection 80 00:07:13,967 --> 00:07:21,011 of artist entity objects to a collection of artistNames, 81 00:07:21,011 --> 00:07:27,339 var artistNames = ComicBook.Artists.Select( 82 00:07:27,339 --> 00:07:31,669 a => a.Name).ToList();. 83 00:07:31,669 --> 00:07:36,567 Then I'll use the strings join method to transform the collection of 84 00:07:36,567 --> 00:07:39,277 strings into a comma delimited list. 85 00:07:39,277 --> 00:07:43,849 Var artistsDisplayext = string.join, 86 00:07:43,849 --> 00:07:48,294 the first parameter is the string to use as 87 00:07:48,294 --> 00:07:53,378 the separator that goes in between each value. 88 00:07:53,378 --> 00:07:56,170 Let's use a comma followed by a space. 89 00:07:58,090 --> 00:08:00,980 The second parameter is the collection of values to join. 90 00:08:03,460 --> 00:08:04,448 Artist names, 91 00:08:04,448 --> 00:08:09,470 now I'll write that value to the console using the console right line method. 92 00:08:12,650 --> 00:08:18,950 Console WriteLine (artistsDisplayText). 93 00:08:18,950 --> 00:08:21,763 With those changes in place, let's test our app. 94 00:08:27,970 --> 00:08:32,368 And here's our three comic books along with their respective artists. 95 00:08:32,368 --> 00:08:36,296 The Amazing Spider-Man number one, Stan Lee, Steve Ditko, 96 00:08:36,296 --> 00:08:38,675 The Amazing Spider-Man number two. 97 00:08:38,675 --> 00:08:41,225 Again with Stan Lee and Steve Ditko, and 98 00:08:41,225 --> 00:08:45,290 the Invincible Iron Man number one with Stan Lee and Jack Kirby. 99 00:08:46,290 --> 00:08:49,699 Now let's review the database that EF generated for 100 00:08:49,699 --> 00:08:52,810 us using the SQL server opted explorer window. 101 00:08:58,340 --> 00:09:03,560 There are two new tables artist comic books and artists. 102 00:09:07,796 --> 00:09:11,190 The artist table is exactly what we'd expect it to be. 103 00:09:11,190 --> 00:09:16,673 It contains columns for each of our artist entitiy class 104 00:09:16,673 --> 00:09:21,680 properties ID and name. 105 00:09:21,680 --> 00:09:28,410 The artist comicbooks table contains two columns artist ID and comic book ID. 106 00:09:28,410 --> 00:09:33,460 Interestingly this table doesn't have a corresponding indie class in our model. 107 00:09:33,460 --> 00:09:36,740 EF automatically added this table to the database 108 00:09:36,740 --> 00:09:41,710 in order to store the artist to kind books many to many relationship data. 109 00:09:41,710 --> 00:09:46,580 This table is known by a variety of names including junction, 110 00:09:46,580 --> 00:09:51,190 join, linking, mapping, or bridge. 111 00:09:51,190 --> 00:09:52,951 Let's review the tables data. 112 00:09:55,059 --> 00:09:58,350 We can sort the data to make it easier to review. 113 00:09:58,350 --> 00:10:02,708 Click the sort and filter dataset tool bar icon and 114 00:10:02,708 --> 00:10:06,557 set the sort for both columns to ascending. 115 00:10:10,517 --> 00:10:11,470 Then click OK. 116 00:10:13,960 --> 00:10:18,581 Now we can see that artist ID 1 is associated with 117 00:10:18,581 --> 00:10:22,086 comic book I.D.'s 1, 2 and 3. 118 00:10:22,086 --> 00:10:28,470 And artist ID 2 is associated with comic book I.D.'s 1 and 2. 119 00:10:28,470 --> 00:10:32,935 And artist ID 3 is associated with comic book ID 3.