9.0: Adding the Follow Entity
Last updated
Last updated
Currently, we've added two tables to our database: Users and Photos. These have a one-to-many relationships (one user has many photos). Entity Framework is able to handle this for us with default conventions.
Here's a reminder of the diagram of our current schema:
We're now going to add a many-to-many relationship where many users can 'follow' many other users. We'll do this by adding a Follow
entity that holds the two IDs of User
s - one for the 'follower' and one for the 'followee. This is called a
Join Entity`. Here is more information in the docs.
Our Follow
entity will sit between the two users and create two one-to-many relationships instead of having one many-to-many relationships.
Inside of the .Data/Entities
folder, add a new class called Follow.cs.
Again, this entity will just hold the relationship between two different User
s - you can think of it as a 'following event'. When one User
follows another - they are the 'follower' and the user they're following is the 'followee.' If the 'followee' follows the 'follower' back - that a new entity is created and their roles are reversed.
This can be confusing - especially the semantics of 'follower' and 'followee' but, it's important to keep straight.
Next, we'll add two navigation properties to the User
entity. One collection will hold the other User
s that the entity is 'following' and one will hold the User
s that 'follow' the User
.
These relationships can be a bit tricky to wrap our head around. They're also confusing for Entity Framework! We'll need to set up configuration so that EF Core will create the relationships correctly.
Open up EFConnectContext.cs
from the .Data
project.
We'll add one DbSet
property called Follows
to store the User
s the entity follows:
Next, we'll add an override
for the OnModelCreating()
method of the DbContext
class. This is the method that is called when an entity is created.
We're telling EF Core that when a Follow
entity is created - make the Primary Key by combining the FollowerId
and FolloweeId
.
We're configuring the relationship by telling EF Core that one Followee
has many Follower
s and the foreign key for this should be the FollowerId
. We're also restricting delete behavior so that if one user is deleted - users with relationships to that user aren't also deleted.
We're doing the same as in step (2), but we're defining the relationship in the other direction. We're telling EF Core that one Follower
has many Followee
s.
Stop your .API
project from running if it is currently. In your terminal, navigate to the .Data
project and run a new migration:
Next, update the database:
Here's what our database should currently look like: