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