2012-03-21 56 views
1

消息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屬性User POCO和User Sender,User Receiver屬性Message POCO。 我該怎麼做?

回答

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; } 
} 
相關問題