在同一个表上有许多对多个列

时间:2012-03-21 07:06:00

标签: entity-framework c#-4.0 entity-framework-4 many-to-many

消息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; }

}

SenderIDReceiverIDUserID表格中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 SenderUser Receiver属性POCO。 我该怎么做?

1 个答案:

答案 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; }
}