我有一个 EF 模型,其中两个实体具有 M 对 M 关系,例如:
public class User
{
public int UserId { get; set; }
[System.ComponentModel.DataAnnotations.Schema.InverseProperty("Received")]
public virtual System.Collections.Generic.ICollection<Message> ReceivedMessages { get; set; }
}
public class Message
{
public int MessageId { get; set; }
public virtual System.Collections.Generic.ICollection<User> Received { get; set; }
public Message()
{
this.Received = new System.Collections.Generic.List<User>();
}
}
有时我有 2 个用户的 ID,并想检查是否有仅与这 2 个用户相关的消息:
B.Models.User U1 = db.Users.Single(u => u.UserId == ID1);
B.Models.User U2 = db.Users.Single(u => u.UserId == ID2);
B.Models.Message header = db.Messages.Single(m => m.Received.Count() == 2 && m.Received.Contains(U1) && m.Received.Contains(U2));
但是我得到一个错误,在这种情况下只能使用原始值。我没有包含 UserId 和 MessageId 的实体,那么我该如何实现呢?