Entity Framework Core Relationships – Session 47

EF Core Relationship

During this session, we are going to talk about the relationship between entities in Entity Framework Core, and demonstrate different types of relationships. Generally, there are three types of relationships which are one-to-one/zero, one-to-many, and many-to-many that we discuss all of them in this session.

EF Core Relationship

One-to-one/zero Relationship in EF Core

If a record of entity A has just a connection with one/zero record of entity B, thus entity A and B have one-to-one/zero relationship. For instance, each Actor has just a type of gender, thus the Actor entity has one-to-one/zero relationship with the Gender entity. Also, if we decorate the GenderID property with Required data annotation which is Foreign Key inside the Actor Property, we make it one-to-one as we should select a gender for each Actor.

one-to-one/zero relationship in EF Core

As you can see, for making the one-to-one relationship, we need to do some changes to our domain classes (Models). In this scenario, GenderID in the Actor entity is a Foreign Key which has a one-to-one relationship with the Gender entity. Moreover, we can decorate this Virtual property to ForeginKey data annotation as you can see. Also, we need to add a Virtual Public property inside the Actor entity with the type of Gender. Next, we need to add a Virtual Public property inside the Gender entity with the type of Actor.

One-to-many Relationship in EF Core

When a record of entity A has relation with more than one record of entity B, entity A has a one-to-many relationship with entity B. For example, each Actor can win several Awards, thus the Actor entity has a one-to-many relationship with the Awards entity.

one-to-many relationship in EF Core

As you can see, we need to add Foreign Key (FK) to the Award entity which is ActorID. Also, it needs to add a Public Virtual property with the type of Actor, inside the Award entity. Moreover, inside the Actor entity, we need to add a Public Virtual ICollection of the Award entity.

Many-to-many Relationship in EF Core

In the situation that several records of entity A have a relationship with several records of entity B, we have a many-to-many relationship between entity A and B. For instance, each Actor can play role in several Movies, also each Movie can have several Actors. Hence, Actors and Movies entities have a many-to-many relationship with each other.

For implementing this relationship, we just need to add a Public Virtual ICollection on respective entities to each other. Then, after the Migration, a third Table will be created inside the Database which contains FK of this relationship.

For your reference, the above-mentioned example is available on my GitHub which you can find it HERE.

Make Changes in our Web App

Also in this session, we do some cleanup inside Daily Cost Web App which is our main project during this tutorial to make it ready for upcoming sessions. Also, we change Cost and Category entities and create a one-to-one relationship between them. You can find below the code which is related to this relationship.

Cost Model

Category Model

Also, we changed the Payment Method type to an enum class which its code is available below.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE.

0.00 avg. rating (0% score) - 0 votes

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments