Using the Context5:03 with James Churchill
Let's update the "Program.cs" file to use our context class to add a comic book, retrieve a list of comic books, and write the comic book series titles to the console.
To follow along commiting your changes to this course, you'll need to fork the dotnet-comic-book-gallery-model repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-comic-book-gallery-model git checkout tags/v1.7 -b using-the-context
F12- Go To Definition
ALT+F12- Peek Definition
Let's update the program.CS file to use our context class to add a comic book, 0:00 retrieve a list of comic books, and 0:06 write the comic book series titles to the console. 0:08 To start, we need to instantiate an instance of our context class. 0:11 Var context = new context. 0:15 If we press F12, to look at the definition of our context class, 0:22 and F12 again to look at the definition for the DbContext class, 0:26 we can see that the DbContext, implements the IDisposable interface. 0:32 Press Alt, F12 to peak the definition for the IDisposable interface. 0:37 The i-disposable interface provides a mechanism for 0:42 releasing unmanaged resources through its single method dispose unmanaged 0:45 resources need to be released or cleaned up in order to prevent memory leaks. 0:51 Applications that have memory leaks will gradually over time consume more and 0:56 more memory which will cause the application to slow down. 1:00 And potentially even slow down the machine that the application is hosted on. 1:03 When we use our contacts to persist, or retrieve data from the database, 1:10 EF will open a connection to the database, which is an unmanaged resource. 1:14 By calling our context's dispose method, which is inherited from the DB context 1:19 base class, we're letting EF know that the database connection can be closed. 1:24 We can easily ensure that the context is dispose method will 1:30 be called by placing the instantiation of the context within a using statement. 1:33 Using open parenthesis close parenthesis and then a set curly braces. 1:43 Now we can use the context to add a comic book to the database. 1:50 To start we need to call the add method on the context as comic books DB set 1:54 property. 1:58 And pass in an instance of the comic book model. 1:59 Context.ComicBooks.Add(new ComicBook). 2:02 Don't forget to add the using directive, for 2:15 the ComicBookGalleryModel.Models name space. 2:18 Then I'll set the series title property to 2:21 The Amazing Spiderman Issue number to 1 and 2:27 published on to DateTime today. 2:32 Then we need to call the context of the save changes method 2:39 to persist the added entitiy to the database. 2:42 After adding the comic book to the database let's retrieve 2:47 a list of the comic books from the context. 2:50 We can do that by calling the to list method on the contexts 2:53 comic books DB set property var comcBooks, 2:59 = context.ComicBooks.ToList(). 3:04 This will retrieve all of the comic books from the database, in no particular order. 3:09 We'll see later how to filter and order the results. 3:14 We can use it for each loop to a enumerate the collection of comic books and 3:18 write each comic book series title property value to the consul. 3:23 Foreach var comic book in comic 3:27 books then console.write 3:33 line comicbook.seriestitle. 3:38 Let's make a call to the console's read line method so 3:44 that the command window will stay open until we press the enter key. 3:47 Console, read line. 3:51 Press F5 to build and start the app. 3:55 Here's the text The Amazing Spider-Man written to the console. 4:03 I'll press the Enter key to let the app finish executing. 4:08 Then I'll press F5 to run the app again. 4:12 Now we're seeing the Amazing Spider-Man being written to the console not once but 4:15 twice. 4:20 Why is this happening? 4:22 Well we ran the app twice. 4:23 So we added a comic book to the database twice. 4:25 If we press enter and 4:29 run our app again we'll see the text written three times to the Consul. 4:30 This confirms that we can successfully add a comic book to the database and 4:34 retrieve the complete list of comic books. 4:39 Our application is persisting data, but, how is this working? 4:42 And where is our database? 4:46 We'll answer these questions the next section. 4:48 If you're following along and 4:51 ran into any issues, be sure to continue on to the next section. 4:52 We'll start with ensuring that you have SQL Server LocalDB installed and 4:57 configured correctly. 5:00 See you then. 5:02
You need to sign up for Treehouse in order to download course files.Sign up