问题
In my MVC application, I have an entity relation between Student and Lookup tables with StatusID-LookupKey and GenderID-LookupKey pairs inestead of (StatusID-ID and GenderID-ID pairs). However, not being able to define the LookupKey column as the matching parameter with the StatusID and GenderID, I have to use ID column of Lookup table. How can I use InverseProperty and match the related columns to the LookupKey column? Thanks in advance.
Here are the Lookup table and the entities below:
Lookup Table:
ID | LookupType | LookupKey | LookupValue |
1 | Status | 0 | Passive |
2 | Gender | 1 | Male |
3 | Status | 1 | Active |
4 | Gender | 2 | Female |
5 | Status | 2 | Cancelled |
Lookup Entity:
public class Lookup
{
[Key]
public int ID { get; set; }
public string LookupType { get; set; }
public int LookupKey { get; set; }
public string LookupValue { get; set; }
public virtual ICollection<Student> Students { get; set; }
//This works when using PK (ID) as matching column.
public ICollection<Student> StatusLookupStudents { get; set; }
public ICollection<Student> TermLookupStudents { get; set; }
}
Student Entity:
public class Student
{
[Key]
public int ID { get; set; }
//Foreign key for Status
public int StatusID { get; set; }
//Foreign key for Gender
public int GenderID { get; set; }
//This works when using PK (ID) as matching column. But I want to use LookupKey column for matching
[ForeignKey("StatusID")]
[InverseProperty("StatusLookupStudents")]
public virtual Lookup StatusLookup { get; set; }
[ForeignKey("TermID")]
[InverseProperty("TermLookupStudents")]
public virtual Lookup TermLookup { get; set; }
}
来源:https://stackoverflow.com/questions/29327772/how-to-define-matching-column-in-inverseproperty