I have been struggling with a "one to many" relationship regarding a database view.
Im using entity framework and I have this "simplified" model:
A "Course" entity. A "Student" entity.
A Course can have many Students.
The following code works fine:
public class Course
{
public int Id { get; set; }
public virtual List<Student> Students { get; set; }
}
public class Sudent
{
public int CourseId { get; set; }
public virtual Course Course { get; set; }
}
private List<Course> GetCourses()
{
List<Course> courses;
using (var db = context ?? new MyContext())
{
courses = (from course in db.Courses select course)
.Include(l => l.Students)
.ToList();
}
}
Here is my MyContext:
public class MyContext : DbContext
{
protected MyContext()
{}
public virtual DbSet<Course> Courses { get; set; }
public virtual DbSet<OpenCourse> OpenCourses { get; set; }
}
I have a view "OpenCourses" that gets all courses that are open.
public class OpenCourse
{
public int Id { get; set; }
public virtual List<Student> Students { get; set; }
}
private List<OpenCourse> GetOpenCourses()
{
List<Course> courses;
using (var db = context ?? new MyContext())
{
courses = (from course in db.OpenCourses select course)
.Include(l => l.Students)
.ToList();
}
}
When I run it I get the following inner-exception: "Invalid column name 'OpenCourse_Id'."
I get that I somehow have to associate the OpenCourse-view with many Students. But how?
Prefereably I would like to do this in my method and my lambda expression.