title: Entity Framework 6 Lazy Loading not working
body: <p>I am having some trouble with EF6 lazy loading, code first to an existing database.</p>
  
  <p>Here are the Entities that are giving me the issue, I have no idea why it is not working, everything I find online says it should be working.</p>
  
  <pre><code>public class User
  {
      public long UserId { get; set; }
      public string Email { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public virtual ICollection&lt;Token&gt; Tokens { get; set; }
      public virtual ICollection&lt;Business&gt; Businesses { get; set; }
  
      public virtual ICollection&lt;Candidate&gt; Candidates { get; set; }
  }
  </code></pre>
  
  <p>Here is the configuration mappings:</p>
  
  <pre><code>public class Token
  {
      public long TokenId { get; set; }
      public long UserId { get; set; }
      public Guid TokenValue { get; set; }
      public DateTime ExpirationDate { get; set; }
      public virtual User User { get; set; }
  }
  
      public TokenMap()
      {
          this.HasKey(t =&gt; t.TokenId);
  
          this.Property(t =&gt; t.TokenValue)
              .IsRequired();
          this.Property(t =&gt; t.ExpirationDate)
              .IsRequired();
  
          this.ToTable("Tokens");
          this.Property(t =&gt; t.TokenId).HasColumnName("TokenId");
          this.Property(t =&gt; t.UserId).HasColumnName("UserId");
          this.Property(t =&gt; t.TokenValue).HasColumnName("TokenValue");
          this.Property(t =&gt; t.ExpirationDate).HasColumnName("ExpirationDate");
  
          this.HasRequired(s =&gt; s.User)
              .WithMany(s=&gt;s.Tokens)
              .HasForeignKey(s=&gt;s.UserId);
      }
  
  
      public UserMap()
      {
          this.ToTable("Users");
          this.HasKey(t =&gt; t.UserId);
          this.Property(t =&gt; t.Email)
              .IsRequired();
          this.Property(t =&gt; t.FirstName)
              .IsRequired();
          this.Property(t =&gt; t.LastName)
              .IsRequired();
  
          this.HasMany(t =&gt; t.Businesses)
              .WithMany(set =&gt; set.Users)
              .Map(m =&gt;
              {
                  m.ToTable("BusinessUser");
                  m.MapLeftKey("UserId");
                  m.MapRightKey("BusinessId");
              });
  
          this.HasMany(s =&gt; s.Tokens)
              .WithRequired(s =&gt; s.User)
              .HasForeignKey(s =&gt; s.UserId);
  
          this.HasMany(s =&gt; s.Candidates)
              .WithOptional(s =&gt; s.User)
              .HasForeignKey(s =&gt; s.UserId);
      }
  </code></pre>
  
  <p>And here is a few snippets from the context:</p>
  
  <pre><code>    public DbSet&lt;Token&gt; Token { get; set; }
      public DbSet&lt;User&gt; User { get; set; }
  
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
  
          modelBuilder.Configurations.Add(new TokenMap()); 
          modelBuilder.Configurations.Add(new UserMap());
  
      }
  </code></pre>
  
  <p>Whenever I do a SingleOrDefault on the Tokens entity the result' user is null.</p>
  
  <p>Any idea what I am doing wrong? All the data in the database is right, and UserId does have a value.</p>
  
  <p>Here is a more elaborate example of calling:</p>
  
  <p>I am implementing the repository patern.</p>
  
  <p>In the class doing the call' constructor I have:</p>
  
  <pre><code>context = new Consolid8ContextProvider();
  uow = new UnitOfWork(context);
  </code></pre>
  
  <p>And then <code>uow.Tokens.First(u =&gt; u.ExpirationDate &gt; DateTime.Now &amp;&amp; u.TokenValue == token);</code></p>
  
  <p>Tokens is my TokenRepository that exposes the Tokens entity, and First is a wrapper for FirstOrDefault.</p>
  
  <p>This results in a token object with all of the properties set except for the User navigation property</p>