Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding Our Context3:03 with James Churchill
Let's add a context class to our project, so we can persist and retrieve data from our database.
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.6 -b adding-our-context
Before we can persist and 0:00 retrieve data from our database, we need to add a context class to our project. 0:01 The context class is our gateway to the database. 0:06 All communication from our application to the database flows through the context. 0:09 The context defines the available Entity sets and 0:14 manages the relationships between those entities. 0:17 It's used to retrieve entities from the database, persist new and 0:21 changed entities to the database, and even to remove entities from the database. 0:25 When retrieving entities from the database, the context is responsible for 0:30 materializing the data from the database into Entity Object Instances. 0:34 The context also caches those entity object instances for its lifetime, however 0:39 short or long that might be so that it can track changes to those entities. 0:44 As we learn about EF and develop our projects, 0:50 we'll interact with the context again and again. 0:52 Let's see how to add a context class to our project. 0:56 Right-click on the project and select Add > Class. 0:59 Name the class Context and click Add. 1:05 Just like we did with the entity class, go ahead and add the public access modifier. 1:11 Then inherit from the EF DbContext class. 1:17 Visual Studio will complain that it can't find the type. 1:23 So go ahead and add the missing using statement for 1:26 the System.Data.Entity namespace. 1:29 The DbContext class is a higher level abstraction of EF's object context class. 1:34 before the DbContext class was added to EF, 1:40 object context was used to load and persist entities. 1:44 While object context isn't deprecated, 1:48 it's almost never used directly now that we have the DbContext class. 1:51 Given that, we'll focus on learning how to use the DbContext class. 1:56 Our context class needs to contain a collection of Db set properties. 2:00 One property for each indie that we need to write queries for. 2:05 Let's add a Db set property for the ComicBook entity. 2:09 Public DbSet of type ComicBook. 2:14 Add the missing namespace, or ComicBookGalleryModel.Models, and 2:20 use the plural version of our entity class name for the property name, ComicBooks. 2:27 While not necessary, using the plural version of the entity class name 2:34 is a common convention for DB set property names. 2:37 Often, you'll add a DB set property for 2:41 each entity class that you have in your model. 2:43 But sometimes you won't need to add a DB set property for an entity. 2:46 We'll see an example of that later in this course. 2:50 For now, this is all of the code that our context class needs to contain. 2:53 Next, we'll update our console app to persist and 2:57 retrieve data using our context. 3:00
You need to sign up for Treehouse in order to download course files.Sign up