0
我有一個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的實體,所以我該如何實現?