同一张桌子上有多个外键?

时间:2016-11-07 14:40:15

标签: asp.net asp.net-mvc entity-framework

我正在尝试将外键保存在一行的两列中。出于某种原因,这表现得很糟糕,因为有些导航属性在我的表中显示为列...这是模型。我做错了什么?

 public class BorrowedPerson
    {
        public int BorrowedPersonID { get; set; }
        public Nullable<int> PersonID { get; set; }
        public Nullable<int> CompanyID { get; set; }
        public Nullable<int> TempCompanyID { get; set; }
        public virtual Person Person { get; set; }
        public virtual Company Company { get; set; }
        public virtual Company TempCompanyID { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

我想通了......非常简单的回答。当您为同一类型指定多个导航属性时,它需要您手动指定外键,如此。

public class BorrowedPerson
    {
        public int BorrowedPersonID { get; set; }
        public Nullable<int> PersonID { get; set; }
        public Nullable<int> CompanyID { get; set; }
        public Nullable<int> TempCompanyID { get; set; }
        public virtual Person Person { get; set; }
        [ForeignKey("CompanyID ")]
        public virtual Company Company { get; set; }
        [ForeignKey("TempCompanyID")]
        public virtual Company TempCompanyID { get; set; }
    }