title: Lazy loading in Entity Framework with the repository pattern
body: <p>I have a entity like:</p>
  <pre><code>public class Doctor : User
  {
      public Doctor(string userName, string firstName, string lastName,
          string mobileNumber, string email, Sexes sex, Role myRole, DoctorExpertises expertise)
          : base(userName, firstName, lastName, mobileNumber, email, sex, myRole)
      {
          this.Expertise = expertise;
          this.Results = new List&lt;Result&gt;();
      }
  
      private Doctor()
      {
          this.Results = new List&lt;Result&gt;();
      }
  
      public void AddResult(Result result)
      {
          this.Results.Add(result);
      }
  
      public DoctorExpertises Expertise { get; private set; }
  
      private ICollection&lt;Result&gt; results;
  
      public virtual ICollection&lt;Result&gt; Results
      {
          get { return results; }
          private set { results = value; }
      }
  }
  </code></pre>
  <p>And I have typical repository like:</p>
  <pre><code>public abstract class RepositoryBase&lt;T&gt; where T : class
  {
      private DbContext dataContext;
      protected readonly IDbSet&lt;T&gt; dbset;
  
      protected RepositoryBase(IDatabaseFactory databaseFactory)
      {
          DatabaseFactory = databaseFactory;
          dbset = DataContext.Set&lt;T&gt;();
      }
  
      protected IDatabaseFactory DatabaseFactory
      {
          get;
          private set;
      }
  
      protected DbContext DataContext
      {
          get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
      }
  
      public virtual void Add(T entity)
      {
          dbset.Add(entity);
      }
  
      public virtual void Update(T entity)
      {
          dbset.Attach(entity);
          dataContext.Entry(entity).State = EntityState.Modified;
      }
  
      public virtual void Delete(T entity)
      {
          dbset.Remove(entity);
      }
  
      public virtual void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; where)
      {
          IEnumerable&lt;T&gt; objects = dbset.Where&lt;T&gt;(where).AsEnumerable();
          foreach (T obj in objects)
              dbset.Remove(obj);
      }
  
      public virtual T GetById(long id)
      {
          return dbset.Find(id);
      }
  
      public virtual T GetById(string id)
      {
          return dbset.Find(id);
      }
  
      public virtual IEnumerable&lt;T&gt; GetAll()
      {
          return dbset.ToList();
      }
  
      public virtual IEnumerable&lt;T&gt; GetMany(Expression&lt;Func&lt;T, bool&gt;&gt; where)
      {
          return dbset.Where(where).ToList();
      }
  
      public T Get(Expression&lt;Func&lt;T, bool&gt;&gt; where)
      {
          return dbset.Where(where).FirstOrDefault&lt;T&gt;();
      }
  }
  </code></pre>
  <p>And I want lazy load one result from doctor with below test code:</p>
  <pre><code>[TestMethod]
  public void TestShouldLoadPropertyIfLazyLoaded()
  {
      // Act
      Doctor doctor = this.userRepository.Get(u =&gt; u.UserName == &quot;soroosh&quot;) as Doctor;
      Result result = doctor.Results.FirstOrDefault();
  
      // Asserts
      Assert.IsNotNull(doctor);
      Assert.IsNotNull(result);
  }
  </code></pre>
  <p>Unfortunately it doesn’t work correctly. <code>doctor</code> is not null, but result is null.</p>
  <p>If I use eager loading with <code>include</code> method it loads correctly, but I definitely want to use lazy loading.</p>
  <p>I have seen <a href="https://stackoverflow.com/questions/15660619/entity-framework-lazy-loading-with-generic-repository">this question</a> and other questions like this, but none of them wasn't useful, because all of them use eager loading to solve problem. Eager loading is not suitable for us, because in my <a href="https://stackoverflow.com/q/19983814/1539100">main problem</a> it seems eager loading is not possible.</p>
  <p>What should I do?</p>