10.0: Adding Message Entity
We're going to build a messaging system. We'll start by adding a Message
entity that will act as a join entity similarly to our Follow
entity.
Add Message Entity
In your .Data/Entities
folder, add a new class called Message.cs
:
We have the join properties - SenderId
/Sender
and RecipientId
/Recipient
. But, we're also collecting more information about the Message
than we did with the Follow
. Content
, IsRead
, DateRead
, and DateSent
are pretty self explanatory.
However, we have separate properties for SenderDeleted
and RecipientDeleted
. If one user deletes a message - we don't want to delete it for the other user as well. We'll only delete the message from the database if both users delete it.
Updating User Entity
We'll add two collection navigation properties on our User
entity: one for messages received and one for messages sent:
Updating EFConnectContext
Next, we need to update our DbContext
. We'll start by adding a DbSet
property:
Next, we'll configure it in the OnModelCreating()
method:
Run Migration and Update Database
In your terminal, navigate to the .Data
project and run the following command to add a new migration:
Take a look at the migration and make sure it looks right, then run the following command to update the database:
Here's an updated database diagram:
Last updated