我正在使用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
属性中,但是由于从成员表中删除了StaffId
,所以该方法不起作用。还有其他建议吗?
答案 0 :(得分:2)
这应该可以解决问题
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; }
}