使用主 table 键链接到不同的 table 作为 code-first 中的外键
Using master table key for linking to different table as foreign key in code-first
我有一个使用 code-first 和 MVC5 的问题。这是我的场景:
Simple Model
我有 LeaveApplication header table,其中包含 CompanyCode 和 WorkGroup 字段。并且,有一个详细信息 table LeaveApplicationDetails,其中包含 LeaveTypeCode。现在,LeaveTypes 在另一个模型 LeaveType 中定义,它有键 {CompanyCode, WorkGroup, LeaveTypeCode}。
现在我如何使用 code-first 中的 LeaveApplicationHeader 模型字段 link LeaveApplicationDetails 模型和 LeaveType 模型?
编辑:
示例 C# 代码:
public class LeaveTypes
{
[Key, Column(Order = 0)]
public int CompCode { get; set; }
[Key, Column(Order = 1)]
public int WrkGrp { get; set; }
[Key, Column(Order = 2)]
[Required]
[StringLength(2)]
public string LeaveTypeCode { get; set; }
[StringLength(50)]
public string LeaveTypeName { get; set; }
}
public class LeaveApplicationHeader
{
[Key]
public int LeaveAppId { get; set; }
public int EmpUnqId { get; set; }
[ForeignKey("EmpUnqId")]
public Employees Employee { get; set; }
public int CompCode { get; set; }
[ForeignKey("CompCode")]
public Company Company { get; set; }
public int WrkGrp { get; set; }
[ForeignKey("CompCode, WrkGrp")]
public WorkGroups WorkGroup { get; set; }
}
public class LeaveApplicationDetails
{
[Key, Column(Order = 0)]
public int LeaveAppId { get; set; }
[Key, Column(Order = 1)]
public int LeaveAppItem { get; set; }
// HOW TO REFER LeaveTypeCode here?
// Using CompCode and WrkGroup of LeaveApplicationHeader?
}
Now how I can I link LeaveApplicationDetails model with LeaveType model by using LeaveApplicationHeader model fields in code-first?
你不能。出于同样的原因,您不能在数据库中拥有外键。要么将所有外键列放在一个 table 上(例如,通过创建 LeaveApplicationHeader 的键(CompCode ,WrkGrp ,LeaveAppId ),或者在 LeaveApplicationDetail 上写一个 属性 以根据需要查询描述。
我有一个使用 code-first 和 MVC5 的问题。这是我的场景:
Simple Model
我有 LeaveApplication header table,其中包含 CompanyCode 和 WorkGroup 字段。并且,有一个详细信息 table LeaveApplicationDetails,其中包含 LeaveTypeCode。现在,LeaveTypes 在另一个模型 LeaveType 中定义,它有键 {CompanyCode, WorkGroup, LeaveTypeCode}。
现在我如何使用 code-first 中的 LeaveApplicationHeader 模型字段 link LeaveApplicationDetails 模型和 LeaveType 模型?
编辑: 示例 C# 代码:
public class LeaveTypes
{
[Key, Column(Order = 0)]
public int CompCode { get; set; }
[Key, Column(Order = 1)]
public int WrkGrp { get; set; }
[Key, Column(Order = 2)]
[Required]
[StringLength(2)]
public string LeaveTypeCode { get; set; }
[StringLength(50)]
public string LeaveTypeName { get; set; }
}
public class LeaveApplicationHeader
{
[Key]
public int LeaveAppId { get; set; }
public int EmpUnqId { get; set; }
[ForeignKey("EmpUnqId")]
public Employees Employee { get; set; }
public int CompCode { get; set; }
[ForeignKey("CompCode")]
public Company Company { get; set; }
public int WrkGrp { get; set; }
[ForeignKey("CompCode, WrkGrp")]
public WorkGroups WorkGroup { get; set; }
}
public class LeaveApplicationDetails
{
[Key, Column(Order = 0)]
public int LeaveAppId { get; set; }
[Key, Column(Order = 1)]
public int LeaveAppItem { get; set; }
// HOW TO REFER LeaveTypeCode here?
// Using CompCode and WrkGroup of LeaveApplicationHeader?
}
Now how I can I link LeaveApplicationDetails model with LeaveType model by using LeaveApplicationHeader model fields in code-first?
你不能。出于同样的原因,您不能在数据库中拥有外键。要么将所有外键列放在一个 table 上(例如,通过创建 LeaveApplicationHeader 的键(CompCode ,WrkGrp ,LeaveAppId ),或者在 LeaveApplicationDetail 上写一个 属性 以根据需要查询描述。