1 00:00:00,590 --> 00:00:06,130 Let's update the program.CS file to use our context class to add a comic book, 2 00:00:06,130 --> 00:00:08,180 retrieve a list of comic books, and 3 00:00:08,180 --> 00:00:11,190 write the comic book series titles to the console. 4 00:00:11,190 --> 00:00:15,830 To start, we need to instantiate an instance of our context class. 5 00:00:15,830 --> 00:00:19,820 Var context = new context. 6 00:00:22,500 --> 00:00:26,820 If we press F12, to look at the definition of our context class, 7 00:00:26,820 --> 00:00:32,190 and F12 again to look at the definition for the DbContext class, 8 00:00:32,190 --> 00:00:37,640 we can see that the DbContext, implements the IDisposable interface. 9 00:00:37,640 --> 00:00:42,790 Press Alt, F12 to peak the definition for the IDisposable interface. 10 00:00:42,790 --> 00:00:45,870 The i-disposable interface provides a mechanism for 11 00:00:45,870 --> 00:00:51,470 releasing unmanaged resources through its single method dispose unmanaged 12 00:00:51,470 --> 00:00:56,910 resources need to be released or cleaned up in order to prevent memory leaks. 13 00:00:56,910 --> 00:01:00,924 Applications that have memory leaks will gradually over time consume more and 14 00:01:00,924 --> 00:01:03,983 more memory which will cause the application to slow down. 15 00:01:03,983 --> 00:01:08,390 And potentially even slow down the machine that the application is hosted on. 16 00:01:10,200 --> 00:01:14,330 When we use our contacts to persist, or retrieve data from the database, 17 00:01:14,330 --> 00:01:19,850 EF will open a connection to the database, which is an unmanaged resource. 18 00:01:19,850 --> 00:01:24,478 By calling our context's dispose method, which is inherited from the DB context 19 00:01:24,478 --> 00:01:29,050 base class, we're letting EF know that the database connection can be closed. 20 00:01:30,180 --> 00:01:33,755 We can easily ensure that the context is dispose method will 21 00:01:33,755 --> 00:01:38,627 be called by placing the instantiation of the context within a using statement. 22 00:01:43,369 --> 00:01:50,460 Using open parenthesis close parenthesis and then a set curly braces. 23 00:01:50,460 --> 00:01:54,220 Now we can use the context to add a comic book to the database. 24 00:01:54,220 --> 00:01:58,640 To start we need to call the add method on the context as comic books DB set 25 00:01:58,640 --> 00:01:59,690 property. 26 00:01:59,690 --> 00:02:02,918 And pass in an instance of the comic book model. 27 00:02:02,918 --> 00:02:08,856 Context.ComicBooks.Add(new ComicBook). 28 00:02:15,194 --> 00:02:18,198 Don't forget to add the using directive, for 29 00:02:18,198 --> 00:02:21,519 the ComicBookGalleryModel.Models name space. 30 00:02:21,519 --> 00:02:27,075 Then I'll set the series title property to 31 00:02:27,075 --> 00:02:32,786 The Amazing Spiderman Issue number to 1 and 32 00:02:32,786 --> 00:02:37,120 published on to DateTime today. 33 00:02:39,280 --> 00:02:42,840 Then we need to call the context of the save changes method 34 00:02:42,840 --> 00:02:45,300 to persist the added entitiy to the database. 35 00:02:47,410 --> 00:02:50,520 After adding the comic book to the database let's retrieve 36 00:02:50,520 --> 00:02:53,280 a list of the comic books from the context. 37 00:02:53,280 --> 00:02:59,664 We can do that by calling the to list method on the contexts 38 00:02:59,664 --> 00:03:04,585 comic books DB set property var comcBooks, 39 00:03:04,585 --> 00:03:09,250 = context.ComicBooks.ToList(). 40 00:03:09,250 --> 00:03:14,420 This will retrieve all of the comic books from the database, in no particular order. 41 00:03:14,420 --> 00:03:18,410 We'll see later how to filter and order the results. 42 00:03:18,410 --> 00:03:23,000 We can use it for each loop to a enumerate the collection of comic books and 43 00:03:23,000 --> 00:03:26,190 write each comic book series title property value to the consul. 44 00:03:27,360 --> 00:03:33,236 Foreach var comic book in comic 45 00:03:33,236 --> 00:03:38,434 books then console.write 46 00:03:38,434 --> 00:03:44,790 line comicbook.seriestitle. 47 00:03:44,790 --> 00:03:47,789 Let's make a call to the console's read line method so 48 00:03:47,789 --> 00:03:51,517 that the command window will stay open until we press the enter key. 49 00:03:51,517 --> 00:03:55,610 Console, read line. 50 00:03:55,610 --> 00:03:58,579 Press F5 to build and start the app. 51 00:04:03,708 --> 00:04:08,490 Here's the text The Amazing Spider-Man written to the console. 52 00:04:08,490 --> 00:04:11,110 I'll press the Enter key to let the app finish executing. 53 00:04:12,210 --> 00:04:14,710 Then I'll press F5 to run the app again. 54 00:04:15,830 --> 00:04:20,860 Now we're seeing the Amazing Spider-Man being written to the console not once but 55 00:04:20,860 --> 00:04:22,000 twice. 56 00:04:22,000 --> 00:04:23,370 Why is this happening? 57 00:04:23,370 --> 00:04:25,560 Well we ran the app twice. 58 00:04:25,560 --> 00:04:29,170 So we added a comic book to the database twice. 59 00:04:29,170 --> 00:04:30,309 If we press enter and 60 00:04:30,309 --> 00:04:34,880 run our app again we'll see the text written three times to the Consul. 61 00:04:34,880 --> 00:04:39,250 This confirms that we can successfully add a comic book to the database and 62 00:04:39,250 --> 00:04:41,290 retrieve the complete list of comic books. 63 00:04:42,310 --> 00:04:46,560 Our application is persisting data, but, how is this working? 64 00:04:46,560 --> 00:04:48,630 And where is our database? 65 00:04:48,630 --> 00:04:50,490 We'll answer these questions the next section. 66 00:04:51,490 --> 00:04:52,980 If you're following along and 67 00:04:52,980 --> 00:04:57,140 ran into any issues, be sure to continue on to the next section. 68 00:04:57,140 --> 00:05:00,940 We'll start with ensuring that you have SQL Server LocalDB installed and 69 00:05:00,940 --> 00:05:02,420 configured correctly. 70 00:05:02,420 --> 00:05:02,940 See you then.