title: Entity Framework 6 context not retrieving navigation properties
body: <p>I have found many other posts but they are nt facing exactly the same problem. And they are using a slightly different code. SO I think it is worth reviewing this.</p>
  
  <p>I´m using EF6 code first, and I created a Client Entity that has some navigation properties.</p>
  
  <p>I´ll post just the relevant code, consider there are a few more properties and foreign keys as well, but not relevant to the problem. Model is generating ok.</p>
  
  <pre><code>public class Client
  {
      public Client()
      {
          JobsExperiences = new Collection&lt;JobsExperience&gt;();
          CapacitationCourses = new Collection&lt;CapacitationCourse&gt;();
          ScholarLevelDetails = new Collection&lt;ScholarLevelDetail&gt;();
          Relatives = new Collection&lt;Relative&gt;();
      }
      public long ClientID { get; set; }
      public virtual ICollection&lt;ScholarLevelDetail&gt; ScholarLevelDetails { get; set; }
  
      public virtual ICollection&lt;JobsExperience&gt; JobsExperiences { get; set; }
  }
  </code></pre>
  
  <p>Now I created a ClientServices class where I put all methods that get or send data from and to the data base., ther I have this code, which is working randomly, I´ll try to explain clearly.</p>
  
  <pre><code>    internal Client GetClient(string userId, bool lazyLoadingEnabled = true)
      {
          using (var context = new ApplicationDbContext())
          {
              context.Configuration.LazyLoadingEnabled=lazyLoadingEnabled;
  
              var client = (from _client in context.Client
                            where _client.ApplicationUserId == userId
                            select _client).FirstOrDefault();
  
              return client;
          }
      }
  </code></pre>
  
  <p>My objective some cases is to retrieve just the client attributes, and sometimes all attributes including navigation properties.</p>
  
  <p>In my controller I have a line like this</p>
  
  <pre><code>var client = uuc.GetClient(user.Id, false);
  </code></pre>
  
  <p>or this</p>
  
  <pre><code>var client = uuc.GetClient(user.Id);
  </code></pre>
  
  <p>When I run the first sentence, the navigation properties are initialized but all has Count=0, even when my DB has records associated. I think, if lazy loading is disabled, it means eager loading is enabled, but it seems not. However, there is no Load() Method in the navigation properties to force load.</p>
  
  <p>When I run the second sentence, the navigation properties throws an exception <strong>'client.ScholarLevelDetails' threw an exception of type 'System.ObjectDisposedException'</strong>. This is thrown one line after the sentence, loking at the navigation properties in the watch. However, and this is the weirdest part, if I step back to the sentence and debug stepping into the method, All navigation properties are loaded correctly.</p>
  
  <p>Why the code behaves differently if running at once than running stepping into the method?
  I presume the using statement scope finishes before that the navigation properties load, but why disabling lay loading doe snot retrieve them either?
  How can I code this to have a consistent behaviour?</p>