0

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.

steavy
  • 1,483
  • 6
  • 19
  • 42
  • Did you try setting context.Configuration.LazyLoadingEnabled to false? – Nick Feb 06 '18 at 13:16
  • No, I didn't, because I have other tables in `MyEntities`. And I don't want to affect them. – steavy Feb 06 '18 at 13:17
  • Also, really consider if you need this - there is a reason why lazy loading is enabled by default and using virtual keyword allows EF to do efficient change tracking. – Vidmantas Blazevicius Feb 06 '18 at 13:17
  • @VidmantasBlazevicius Any links about `virtual` and change tracking would be appreciated. – steavy Feb 06 '18 at 13:18
  • 1
    @steavy https://stackoverflow.com/questions/5597760/what-effects-can-the-virtual-keyword-have-in-entity-framework-4-1-poco-code-fi, also kinda old, but I think still relevant - https://blogs.msdn.microsoft.com/adonet/2009/05/28/poco-in-the-entity-framework-4-part-2-complex-types-deferred-loading-and-explicit-loading/ – Vidmantas Blazevicius Feb 06 '18 at 13:22
  • Lazy loading has nothing to do with eager loading, so yes, it is entirely possible. – crunchy Feb 06 '18 at 13:37

1 Answers1

1

Shortly after asking the question, I found an answer which is dead simple: there is a bug under if(loadCourses) that I was not assigning teacherQuery. Fixed code looks like this and works fine:

if(loadCourses)
{
    teacherQuery = teacherQuery.Include(t => t.Courses);
}

Also would like to mention a useful link that @VidmantasBlazevicius provided. It contains answers on how virtual keyword affects your entities.

steavy
  • 1,483
  • 6
  • 19
  • 42