如何使用实体框架在 C# 中建模消息/聊天系统

Mas*_*ohn 4 model-view-controller asp.net-mvc messaging

我有一个简单的问题

我正在开发聊天系统服务,但我遇到了一些有趣的事情。

我目前是这样操作的:

第一张表:

[Messages] :
SenderID(user),
RecipientID(user),
Content(string) 
Run Code Online (Sandbox Code Playgroud)

当然,每次用户向其他用户发送消息时,我都会将其添加到表中。但我想到如果一个表有一百万行,它会变得一团糟

所以我想到了另一种方式:

第一张表:

[Conversation]
ConversationID,
USER1(user),
USER2(user)
Run Code Online (Sandbox Code Playgroud)

第二个表:

[Messages] in which I have 
ConversationID,
Content(string) 
Run Code Online (Sandbox Code Playgroud)

所以基本上,我问,我应该使用哪种配置?

dun*_*wan 5

下面的方法应该可以帮助你解决问题。这是聊天和消息传递的良好基础,通过聊天,您可以从客户端轮询最近的消息并使用直观的 UI。

信息

Message {
  MessageId,
  FromId, -- Foreign key User.UserId
  ToId, -- Foreign key User.UserId
  Subject, 
  Content,
  Attachment, -- can be null or default to a 0
  DateReceived, -- can be null or default to 1901 or sumin'
  DateRead
  ...
}
Run Code Online (Sandbox Code Playgroud)

用户

User {
  UserId
  UserName
  ...
}
Run Code Online (Sandbox Code Playgroud)

查询

Inbox = Message where ToId = Me.UserId
Sent = Message where FromId = Me.UserId
Conversation = Group by Subject
Attachment = (Simple = Path to attachment file. || Better = DocumentId)
Run Code Online (Sandbox Code Playgroud)

依恋

Document {
  int DocumentId,
  int DocTypeId,
  virtual DocumentType DocumentType,
  string FileName,
  int UserId,
  string mimeType,
  float fileSize,
  string storagePath,
  int OrganizationId,
  string fileHash,
  string ipAddress,
  DateTime DateCreated = DateTime.Now;
}
Run Code Online (Sandbox Code Playgroud)

然后你就会遇到群聊的问题。您是否向组中的每个收件人发送消息,还是创建每个收件人都有权访问的单个消息?

但我们保持简单。