I've a question using code-first with MVC5. Here's my scenario:
I have LeaveApplication header table which has CompanyCode and WorkGroup fields. And, there's a details table LeaveApplicationDetails which has LeaveTypeCode. Now, LeaveTypes are defined in another model LeaveType, which has Keys {CompanyCode, WorkGroup, LeaveTypeCode}.
Now how I can I link LeaveApplicationDetails model with LeaveType model by using LeaveApplicationHeader model fields in code-first?
Edit: Sample C# code:
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?
You can't. For the same reason you can't have a Foreign Key in the database. Either put all of the foreign key columns on one table (eg by making the Key of LeaveApplicationHeader (CompCode ,WrkGrp ,LeaveAppId ), or write a property on LeaveApplicationDetail that queries for the Description as needed.