【发布时间】:2017-10-20 20:41:52
【问题描述】:
我在 .net core 1.1 MVC 中有三个模型。
-
通讯
public class Communication { public Guid CommunicationId { get; set; } public string Subject { get; set; } public bool IsRead { get; set; } = false; public Guid UserId { get; set; } public Guid AssignedTo { get; set; } public DateTime CreatedAt { get; set; } = DateTime.Now; public DateTime UpdatedAt { get; set; } = DateTime.Now; public Nullable<DateTime> DeletedAt { get; set; } public virtual ICollection<Message> Messages {get; set;} } -
消息
public class Message { public Guid MessageId { get; set; } public Guid CommunicationId { get; set; } public Guid SenderId { get; set; } = new Guid(); public Guid ReceiverId { get; set; } = new Guid(); public string Body { get; set; } public DateTime CreatedAt { get; set; } = DateTime.Now; public DateTime UpdatedAt { get; set; } = DateTime.Now; public Nullable<DateTime> DeletedAt { get; set; } } 用户
Messages 表中的SenderID 和ReceiverID 是Users 表中的UserID。
这些模型之间的关系: 通讯 has_many 消息。 消息属于_通信 通过 SenderID 向用户发送消息 通过 ReceiverId 向用户发送消息 blongs_
这是我的控制器的样子:
_context.Communications.Include(c=> c.Messages)
我想生成一个这样的 JSON 对象: {communicationId: 'xxx', messages[{messageId: 'xxx', 发件人:{用户信息在这里},接收者:{用户信息在这里} }
如何将 User 对象包含到 Message 对象中?
感谢您的帮助。
【问题讨论】:
标签: c# .net-core entity-framework-core