Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's add a Series entity to our model and define the relationship between the ComicBook and Series entities.
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/v3.1 -b adding-one-to-many-entity-relationship
Additional Learning
[MUSIC]
0:00
Our entity data model currently only
contains a single entity, ComicBook.
0:04
While the ComicBook entity is
an important part of our model,
0:10
it's not enough on its own
to fully describe our data.
0:13
As we've previously discussed,
each ComicBook belongs to a series.
0:17
Our ComicBook entity class currently
contains a SeriesTitle property that, as
0:22
the name suggests, contains the title of
the series that the comic book belongs to.
0:27
If we want to also have a description for
0:32
the series, we can add another property to
the ComicBook entity, SeriesDescription.
0:34
This would work, but
0:39
now the ComicBook entity has two
properties related to a series.
0:41
And if we have more than one ComicBook
object instance that belongs to the same
0:45
series, the series title and
series description property string values
0:49
would be repeated within
each ComicBook instance.
0:54
To avoid this unnecessary
duplication of values,
0:58
we can add a Series entity to our model.
1:01
The Series entity will have ID,
Title and Description properties.
1:04
Then we can replace the ComicBook
entity series title
1:09
into series description properties
with a reference to the series entity.
1:12
The relationship between a ComicBook and
a series is known as a One-to-Many or
1:16
Many-to-One, depending
on your point of view.
1:21
From the series point of view, a series
contains one or more, or many comics.
1:24
And from the ComicBook's point of view,
a ComicBook belongs to a single or
1:30
one series.
1:35
So from the series point of view,
their relationship is One-to-Many.
1:37
And from the ComicBook's point of view,
it's Many-to-One.
1:41
In addition to
the Many-to-One relationship,
1:45
we can model Many-to-Many and
One-to-One relationships.
1:47
We'll implement a Many-to-Many
relationship later in the section.
1:51
For more information on how
to model a One-to-One, or
1:55
One-to-Zero or One relationship,
see the teacher's notes.
1:58
Let's add a series entity to our model and
2:03
define the relationship between
the ComicBook and series entities.
2:06
Right click on the Models folder and
select Add > Class.
2:10
Name the class Series and click Add.
2:16
Add the public access modifier and
2:23
the following properties,
an int Property named Id.
2:26
This property will contain a unique
value for each series in our system.
2:32
A string property named Title.
2:38
This is the title of the series.
2:41
A string property named Description.
2:45
This is a description of the series.
2:50
Now that we have the series entity class,
2:52
let's update the ComicBook
entity class to make use of it.
2:55
I'll replace the series title property
with the series property of type series.
3:04
By adding this property to
the ComicBook entity class,
3:12
we've defined a Many-to-One relationship
between the ComicBook and series entities.
3:15
Each ComicBook instance can be associated
with exactly one series instance,
3:21
but there's no restriction with
how many ComicBook instances
3:26
a series instance can be associated with.
3:29
In this relationship, the series entity
is the principal and the ComicBook entity
3:32
is the dependent, meaning that
a ComicBook is dependent upon a series.
3:38
EF refers to the series property as
a navigation property, as it allows
3:43
us to navigate the relationship from
the ComicBook entity to the series entity.
3:48
Including an Id property is
important to do, so EF will know
3:55
that we intend the data for the series
entity to be stored in its own table.
4:00
If we didn't include the id property,
4:05
EF would treat the series
class as a complex type.
4:07
And its title and
4:11
description properties would be added
to the ComicBook's database table.
4:12
Complex types give you a way with an EF
to group related sets of properties
4:16
within an entity or across entities.
4:21
For more information about EF complex
types, see the teacher's notes.
4:25
We can also define an navigation
property on the series entity,
4:30
though doing so is optional.
4:34
Since a series can be associated
with more than one ComicBook,
4:35
we need to define the navigation
property as a collection,
4:41
public ICollection of type <ComicBook>
ComicBooks get; set for the property type.
4:47
I prefer to use the ICollection generic
interface instead of a concrete type like
4:55
list of T, in order to expose
the functionality that I want to provide
5:00
without tying myself to a specific
collection implementation.
5:04
We also need to initialize
the collection in a default constructor.
5:09
public Series, then ComicBooks
5:14
= new List<ComicBook>.
5:19
This ensures that the ComicBooks property
is ready to be used immediately after
5:26
instantiating a series entity object.
5:31
Now that we've updated our entities,
5:34
we need to update the program classes main
method to get our app compiling again.
5:36
After the break,
we'll make those changes and
5:41
review how our entity relationship
has affected our database.
5:44
You need to sign up for Treehouse in order to download course files.
Sign up