【问题标题】:Build a model with 2 Foreign Keys to the Same Table使用同一个表的 2 个外键构建模型
【发布时间】:2013-02-03 11:44:19
【问题描述】:

假设我有一个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; }

}

【问题讨论】:

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


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 第一个数据注释有语法错误,但它有效。谢谢。
    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 2017-06-01
    • 2012-02-28
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多