1 00:00:00,630 --> 00:00:01,860 Before we can persist and 2 00:00:01,860 --> 00:00:06,640 retrieve data from our database, we need to add a context class to our project. 3 00:00:06,640 --> 00:00:09,567 The context class is our gateway to the database. 4 00:00:09,567 --> 00:00:14,800 All communication from our application to the database flows through the context. 5 00:00:14,800 --> 00:00:17,684 The context defines the available Entity sets and 6 00:00:17,684 --> 00:00:21,420 manages the relationships between those entities. 7 00:00:21,420 --> 00:00:25,460 It's used to retrieve entities from the database, persist new and 8 00:00:25,460 --> 00:00:30,850 changed entities to the database, and even to remove entities from the database. 9 00:00:30,850 --> 00:00:34,700 When retrieving entities from the database, the context is responsible for 10 00:00:34,700 --> 00:00:39,190 materializing the data from the database into Entity Object Instances. 11 00:00:39,190 --> 00:00:44,630 The context also caches those entity object instances for its lifetime, however 12 00:00:44,630 --> 00:00:48,880 short or long that might be so that it can track changes to those entities. 13 00:00:50,020 --> 00:00:52,515 As we learn about EF and develop our projects, 14 00:00:52,515 --> 00:00:56,050 we'll interact with the context again and again. 15 00:00:56,050 --> 00:00:58,491 Let's see how to add a context class to our project. 16 00:00:59,764 --> 00:01:05,800 Right-click on the project and select Add > Class. 17 00:01:05,800 --> 00:01:09,420 Name the class Context and click Add. 18 00:01:11,390 --> 00:01:16,180 Just like we did with the entity class, go ahead and add the public access modifier. 19 00:01:17,690 --> 00:01:21,205 Then inherit from the EF DbContext class. 20 00:01:23,214 --> 00:01:26,580 Visual Studio will complain that it can't find the type. 21 00:01:26,580 --> 00:01:29,630 So go ahead and add the missing using statement for 22 00:01:29,630 --> 00:01:34,313 the System.Data.Entity namespace. 23 00:01:34,313 --> 00:01:40,736 The DbContext class is a higher level abstraction of EF's object context class. 24 00:01:40,736 --> 00:01:44,590 before the DbContext class was added to EF, 25 00:01:44,590 --> 00:01:48,510 object context was used to load and persist entities. 26 00:01:48,510 --> 00:01:51,200 While object context isn't deprecated, 27 00:01:51,200 --> 00:01:56,050 it's almost never used directly now that we have the DbContext class. 28 00:01:56,050 --> 00:02:00,489 Given that, we'll focus on learning how to use the DbContext class. 29 00:02:00,489 --> 00:02:05,331 Our context class needs to contain a collection of Db set properties. 30 00:02:05,331 --> 00:02:09,273 One property for each indie that we need to write queries for. 31 00:02:09,273 --> 00:02:14,940 Let's add a Db set property for the ComicBook entity. 32 00:02:14,940 --> 00:02:20,970 Public DbSet of type ComicBook. 33 00:02:20,970 --> 00:02:27,730 Add the missing namespace, or ComicBookGalleryModel.Models, and 34 00:02:27,730 --> 00:02:34,060 use the plural version of our entity class name for the property name, ComicBooks. 35 00:02:34,060 --> 00:02:37,840 While not necessary, using the plural version of the entity class name 36 00:02:37,840 --> 00:02:41,280 is a common convention for DB set property names. 37 00:02:41,280 --> 00:02:43,810 Often, you'll add a DB set property for 38 00:02:43,810 --> 00:02:46,360 each entity class that you have in your model. 39 00:02:46,360 --> 00:02:50,590 But sometimes you won't need to add a DB set property for an entity. 40 00:02:50,590 --> 00:02:53,560 We'll see an example of that later in this course. 41 00:02:53,560 --> 00:02:57,650 For now, this is all of the code that our context class needs to contain. 42 00:02:57,650 --> 00:03:00,910 Next, we'll update our console app to persist and 43 00:03:00,910 --> 00:03:02,950 retrieve data using our context.