Referring to this previous post: How to add description to columns in Entity Framework 4.3 code first using migrations?
I have successfully implemented the modified s
Although he did not answer my question, I have to thank Raihan for giving me the idea to check again on how Foreign Keys can be declared. :)
Based on Entity Framework Tutorial, a foreign key can be declared 3 ways:
- [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity.
- [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.
- [ForeignKey(ForeignKeyPropertyName)] on the navigation property in the principal entity.
Also referencing to how a one-many relationship can be declared section in Entity Framework Tutorial website, we can see that the method which I had described in my question is Convention 2.
In order for User Raihan's suggestion to work, I will need to change the one-many declaration to Convention 3, which is to declare a navigational properties at both the School and Student classes. Like below:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public School School { get; set; }
}
public class School
{
public int GradeID { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public ICollection<Student> Student { get; set; }
}
Referencing the foreign key declaration sample in the same website, the Student class needs to be further modified as shown below in order for the (Description) attribute to work:
public class Student
{
[Description("Id of Student")]
[Key]
public int Id { get; set; }
[Description("Name of Student")]
public string Name { get; set; }
[Description("Some Description")]
public int SchoolId {get; set;}
public School School { get; set; }
}
I am still looking for a shorter way to get the column description in so please do reply if you think your solution is more elegant than mine.. :)
As you already understand the foreign key is defined in Student entity and the field is SchoolId so you have to add descriptor there. The Students property in the School entity is the navigation property; a feature from EF to easily get the list of all the students of a particular school.