使用2个外键构建一个模型到同一个表

时间:2013-02-18 22:34:40

标签: c# entity-framework-5 data-modeling

假设我有User模型,现在我想添加Friend模型。我想命名我的FK字段RequestorIDResponderID。我需要使用哪些数据注释或流畅的api映射来防止EF自动生成自己的列?

public class Friend
{
    public int FriendID { get; set; }
    public int RequestorID { get; set; }
    public int ResponderID { get; set; }
    public int FriendStatusID { get; set; }
    public DateTime User1RequestDate { get; set; }
    public DateTime User2ResponseDate { get; set; }
    public virtual FriendStatus FriendStatus { get; set; }


    public virtual User Requestor { get; set; }
    public virtual User Responder { get; set; }

}

1 个答案:

答案 0 :(得分:2)

试试这个

public class Friend
    {
        public int FriendID { get; set; }
        public int RequestorID { get; set; }
        public int ResponderID { get; set; }
        public int FriendStatusID { get; set; }
        public DateTime User1RequestDate { get; set; }
        public DateTime User2ResponseDate { get; set; }
        public virtual FriendStatus FriendStatus { get; set; }

        [ForeignKey("RequestorID")] //fixed
        public virtual User Requestor { get; set; }
        [ForeignKey("ResponderID")]
        public virtual User Responder { get; set; }
    }