> For the complete documentation index, see [llms.txt](https://eleven-fifty-academy.gitbook.io/dotnet-302-core/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eleven-fifty-academy.gitbook.io/dotnet-302-core/10-messaging/10.0-adding-message-entity.md).

# 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`:

```csharp
public class Message
{
    public int Id { get; set; }
    public int SenderId { get; set; }
    public User Sender { get; set; }
    public int RecipientId { get; set; }
    public User Recipient { get; set; }
    public string Content { get; set; }
    public bool IsRead { get; set; }
    public DateTime? DateRead { get; set; }
    public DateTime DateSent { get; set; }
    public bool SenderDeleted { get; set; }
    public bool RecipientDeleted { get; set; }
}
```

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:

```csharp
public ICollection<Message> MessagesSent { get; set; }

public ICollection<Message> MessagesReceived { get; set; }
```

## Updating EFConnectContext

Next, we need to update our `DbContext`. We'll start by adding a `DbSet` property:

```csharp
public DbSet<Message> Messages { get; set; }
```

Next, we'll configure it in the `OnModelCreating()` method:

```csharp
builder.Entity<Message>()
    .HasOne(u => u.Sender)
    .WithMany(u => u.MessagesSent)
    .OnDelete(DeleteBehavior.Restrict);

builder.Entity<Message>()
    .HasOne(u => u.Recipient)
    .WithMany(u => u.MessagesReceived)
    .OnDelete(DeleteBehavior.Restrict);
```

## Run Migration and Update Database

In your terminal, navigate to the `.Data` project and run the following command to add a new migration:

```
dotnet ef migrations add MessageEntity -s "../EFConnect.API/EFConnect.API.csproj"
```

Take a look at the migration and make sure it looks right, then run the following command to update the database:

```
dotnet ef database update -s "../EFConnect.API/EFConnect.API.csproj"
```

Here's an updated database diagram:

![DatabaseDiagram3](/files/-LAU8u1CXTb5RbCkrJaM)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://eleven-fifty-academy.gitbook.io/dotnet-302-core/10-messaging/10.0-adding-message-entity.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
