消息POCO:
[Table("Message")]
public class Message
{
[Key, Column(Order = 0)]
public Int64 SenderID { get; set; }
[Key, Column(Order = 1)]
public Int64 ReceiverID { get; set; }
public String Body { get; set; }
}
SenderID
和ReceiverID
是UserID
表格中User
的外键
用户POCO:
[Table("User")]
public class User
{
[Key]
public long UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
我需要Icollection<Message> Messages
POCO中的多对多关系 User
属性以及{{1}中的User Sender
,User Receiver
属性POCO。
我该怎么做?
答案 0 :(得分:2)
消息POCO:
[Table("Message")]
public class Message
{
[Key, Column(Order = 0)]
[ForeignKey("Sender")]
public Int64 SenderID { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Receiver")]
public Int64 ReceiverID { get; set; }
public String Body { get; set; }
[InverseProperty("SentMessages")]
public virtual User Sender { get; set; }
[InverseProperty("ReceivedMessages")]
public virtual User Receiver { get; set; }
}
用户POCO:
[Table("User")]
public class User
{
[Key]
public long UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Message> SentMessages { get; set; }
public virtual ICollection<Message> ReceivedMessages { get; set; }
}