Reputation: 4675
I'm new to Entity Framework relationship and having trouble with 3 tables though I worked out for 2 tables. Here is my problem
I have following models:
public partial class Student
{
public Student()
{
this.StudentCourses = new HashSet<StudentCourse>();
this.Leaves = new HashSet<Leave>();
}
public int id { get; set; }
public string first_name { get; set; }
public string middle_name { get; set; }
public string last_name { get; set; }
public string password { get; set; }
public string email { get; set; }
public string gender { get; set; }
public string facebook { get; set; }
public string contact_number { get; set; }
public string address { get; set; }
public string admin { get; set; }
public string college { get; set; }
public string status { get; set; }
public System.DateTime createdat { get; set; }
public System.DateTime updatedat { get; set; }
public string descripton { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
public virtual ICollection<Leave> Leaves { get; set; }
}
public partial class StudentCourse
{
public StudentCourse()
{
this.Fees1 = new HashSet<Fee>();
this.Issues = new HashSet<Issue>();
}
public int id { get; set; }
public int student_id { get; set; }
public int course_id { get; set; }
public int fees { get; set; }
public System.DateTime join_date { get; set; }
public string admin { get; set; }
public System.DateTime createdat { get; set; }
public System.DateTime updatedat { get; set; }
public virtual Student Student { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Fee> Fees1 { get; set; }
public virtual ICollection<Issue> Issues { get; set; }
}
public partial class Course
{
public Course()
{
this.StudentCourses = new HashSet<StudentCourse>();
this.CourseContents = new HashSet<CourseContent>();
}
public int id { get; set; }
public string course_name { get; set; }
public int course_fees { get; set; }
public int duration { get; set; }
public string admin { get; set; }
public string status { get; set; }
public System.DateTime createdat { get; set; }
public System.DateTime updatedat { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
public virtual ICollection<CourseContent> CourseContents { get; set; }
}
Now to read data, I used:
List<Student> studentlist = entities.Students.Include("StudentCourses").Where(s => s.status.Equals("active")).ToList<Student>();
Q.1. I want to add Course as well because I want to show the name of Course
Q.2. If I want to read data in view, how can I read course name?
For example:
foreach(var student in studentlist){
student.first_name
student.StudentCourses.fees ( it is not working! )
student.StudentCourses.Courses.course_name ( it is not working! )
}
Upvotes: 0
Views: 194
Reputation: 4381
You're only including StudentCourses
. Try this instead :
List<Student> studentlist = entities.Students.Include("StudentCourses")
.Include("StudentCourses.Fees")
.Include("StudentCourses.Course")
.Where(s => s.status.Equals("active")).ToList();
Upvotes: 1