title: Entity Framework 6: Include is not working randomly
body: <p>I have a following query:</p>
  
  <pre><code>public User GetByEmail(string email, bool includeRoles = true, bool enableLazyLoading = false, bool includeDeleted = true)
  {
      IQueryable&lt;User&gt; dbQuery = null;
      Context.Configuration.LazyLoadingEnabled = enableLazyLoading;
  
      if (includeRoles)
      {
         dbQuery = Content.Include(x =&gt; x.Roles).Include(x =&gt; x.Regions).Where(x =&gt; x.Email == email);
      }
      else
      {
         dbQuery = Content.Where(x =&gt; x.Email == email);
      }
  
      if (!includeDeleted)
      {
         dbQuery = dbQuery.Where(x =&gt; !x.IsDeleted);
      }
  
      return dbQuery.FirstOrDefault();
  }
  </code></pre>
  
  <p>It is called periodically from the Quartz job with default parameters like this:</p>
  
  <pre><code>var user = _userRepository.GetByEmail(email);
  </code></pre>
  
  <p>So including roles, excluding deleted users and without lazy loading.
  And most of the time it's working fine: both user roles and regions collections are loaded.
  But sometimes randomly(there should be some cause but I has not found it) user is returned with empty collections(not null). The same user, same query, same data in the database but the result is different.</p>
  
  <p>I found couple articles about problems with includes(<a href="http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework" rel="nofollow noreferrer">like this</a>) but it seems not relevant to me because I don't use projections or groupings here.</p>
  
  <p>Roles and Regions collections are marked <code>virtual</code> and has <code>ICollection&lt;T&gt;</code> type:</p>
  
  <pre><code> public virtual ICollection&lt;Role&gt; Roles { get; set; }
  </code></pre>
  
  <p>Content is just a shorthand for a set:</p>
  
  <pre><code> public DbSet&lt;T&gt; Content
   {
      get { return Context.Set&lt;T&gt;(); }
   }
  </code></pre>
  
  <p>DbContext is registered as instance per request or per thread. In case of Quartz job executions it should be per thread(Ninject):</p>
  
  <pre><code>kernel.Bind&lt;DbContext&gt;().To&lt;PhotoContext&gt;()
          .InScope(context =&gt; HttpContext.Current ?? StandardScopeCallbacks.Thread(context));
  </code></pre>
  
  <p>We have a lot of jobs which are registered like this:</p>
  
  <pre><code>private static void ScheduleJob&lt;T&gt;(int hour, int minute, ScheduleType type, string jobName = null, string groupName = REPORT_GROUP_DEFAULT, DayOfWeek? dayOfWeek = null, int dayOfMonth = 1, bool test = false)
  {
      Type jobType = typeof(T);
      jobName = jobName ?? $"{type}{jobType.Name}";
  
      IJobDetail existingJob = _Scheduler.GetJobDetail(new JobKey(jobName, groupName));
  
      if (existingJob != null)
          _Scheduler.DeleteJob(existingJob.Key);
  
      JobDetailImpl jobDefinition = new JobDetailImpl(jobName, groupName, jobType);
  
      ITrigger trigger = CreateTrigger(jobName, jobDefinition, groupName, hour, minute, type, dayOfWeek, dayOfMonth, test);
  
      _Scheduler.ScheduleJob(jobDefinition, trigger);
  }
  
  public static void Init()
  {
      _Scheduler = DependencyResolver.Current.GetService&lt;IScheduler&gt;();
      ScheduleJob&lt;UserLastActivityUpdateJob&gt;(1, 0, ScheduleType.Hourly);
      //schedule other jobs
      _Scheduler.Start();
  }
  </code></pre>
  
  <p>Inside jobs dependencies are retrieved from the DependencyResolver:</p>
  
  <pre><code>_userRepository = DependencyResolver.Current.GetService&lt;IUserRepository&gt;();
  </code></pre>
  
  <p>What can cause this issue? </p>