[英]How to setup two foreign keys to the same parent column (Entity Framework)
我正在使用ASP.NET MVC,並且具有以下類模型
位置:
public enum PosType
{
ICC, FP
}
public class Position
{
public int Id { get; set; }
public string Name { get; set; }
public PosType? PosType { get; set; }
}
員工:
public class Staff
{
public int Id { get; set; }
public string StaffName { get; set; }
//FK to Position
public int PositionId { get; set; }
//Navigation property
public virtual Position Position { get; set; }
會員:
public class Member
{
public int Id { get; set; }
public string MemberName { get; set; }
//FK to Staff model which stores the ICC Staff Type value
public int? StaffId { get; set; }
//FK to Staff model which stores the FP Staff Type value
public int? FPNumber { get; set; }
//Navigation property
public virtual Staff Staff { get; set; }
在Member模型上, StaffId
屬性是Staff
模型的外鍵,這是一種非常簡單的方法。 我遇到的問題是如何處理FPnumber
屬性,因為我也希望它也是Staff
模型的外鍵。 我的總體目標是擁有Member.StaffId
屬性存儲庫ICC人員類型和Member.FPNumber
屬性存儲庫FP人員類型。 我嘗試將ForeignKey
屬性添加到FPNumber
屬性中,但是由於從Member表中刪除了StaffId
所以該方法StaffId
。 還有其他建議嗎?
這應該可以解決問題
public class Member
{
public int Id { get; set; }
public string MemberName { get; set; }
//FK to Staff model which stores the ICC Staff Type value
[ForeignKey("Staff")]
public int? StaffId { get; set; }
//FK to Staff model which stores the FP Staff Type value
[ForeignKey("FPStaff")]
public int? FPNumber { get; set; }
//Navigation property
public virtual Staff Staff { get; set; } //Use a better descriptive Name than Staff
public virtual Staff FPStaff{ get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.