I have two tables(Teacher
and Course
) in my database and EF mapping:
public partial class Teacher
{
public long Id { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
And I load a teacher like this:
public Teacher GetTeacher(long id)
{
using(var entities = new MyEntities())
{
var teacher = entities.Teachers.Where(t => t.Id == id).FirstOrDefault();
return teacher;
}
}
Now sometimes I want to load Courses list for my Teacher. So I changed implementation like this:
public Teacher GetTeacher(long id, bool loadCourses)
{
using(var entities = new MyEntities())
{
var teacherQuery = entities.Teachers.Where(t => t.Id == id);
if(loadCourses)
{
teacherQuery.Include(t => t.Courses);
}
return teacherQuery.FirstOrDefault();
}
}
And this worked fine. But after that, I decided to turn off LazyLoading for Courses property since I've decided to control this manually by loadCourses
field.
So I've just removed virtual
from Courses collection:
public ICollection<Course> Courses { get; set; }
This did help to turn off LazyLoading but Include
stopped working and my Courses
collection never loads.
So the question is: is it possible to do eager loading for Courses
collection with LazyLoading disabled?
P.S. I don't actually use entity objects in my application but convert them to my Domain objects. So that's why I decided to use bool loadCourses
field instead of actually using LazyLoading.
Also, I'd like to have one SELECT query(with JOIN, of course) sent to the database instead of two separate SELECTs.