如何使用实体的代码优先方法将连接表数据映射到模型?当我获取聊天消息时,我想获取属于聊天消息的用户信息。我收到错误
无法将 linq.iqueryable 键入到 collections.generic.list
但我无法 ToList() 我的 iqueryable。
List<Models.ChatMessageModel> list = from dbModel in db.ChatMessages
join a in db.Users on dbModel.UserId equals a.UserId
select new Models.ChatMessageModel
{
User = new Models.UserModel
{
UserId = dbModel.UserId,
UserName = a.UserName
},
UserId = a.UserId,
DateSent = dbModel.DateSent,
ChatMessage = dbModel.ChatMessage,
ChatMessageId = dbModel.ChatMessageId
};
聊天消息模型
public class ChatMessageModel
{
[Key]
public Guid ChatMessageId { get; set; }
public virtual Guid UserId { get; set; }
[ForeignKey("UserId")]
public virtual UserModel User { get; set; }
public string ChatMessage { get; set; }
public DateTime DateSent { get; set; }
}