3与实体框架的表关系

时间:2013-11-28 08:02:57

标签: asp.net-mvc entity-framework-4

我是Entity Framework关系的新手,虽然我为2个表制定了问题,却遇到了3个表的问题。这是我的问题

我有以下模特:

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; }
}

现在读取数据,我用过:

List<Student> studentlist = entities.Students.Include("StudentCourses").Where(s => s.status.Equals("active")).ToList<Student>();

Q.1。我也想添加课程,因为我想要显示课程的名称 Q.2。如果我想在视图中读取数据,我该如何阅读课程名称?

例如:

foreach(var student in studentlist){
  student.first_name
  student.StudentCourses.fees       ( it is not working! )
  student.StudentCourses.Courses.course_name   ( it is not working! )
}

1 个答案:

答案 0 :(得分:1)

您只包括StudentCourses。试试这个:

List<Student> studentlist = entities.Students.Include("StudentCourses")
                                             .Include("StudentCourses.Fees")
                                             .Include("StudentCourses.Course")
                                    .Where(s => s.status.Equals("active")).ToList();