title: EntityFramework lazy loading is not working
body: <p>i'm new to EntityFramework 6 and keep on problem in POCO.</p>
  
  <p>Following is my code</p>
  
  <p><strong>Domain.cs</strong></p>
  
  <pre><code>public abstract class BaseEntity
  {
      [Key]
      public long Id { get; set; }
  }
  
  public class User : BaseEntity
  {
      public long UserRoleId { get; set; }
  
      public string Name { get; set; }
  
      public virtual _EntityUserRole UserRole { get; set; }    
  }
  
  public class UserRole : BaseEntity
  {
      public string Name { get; set; }
  
      public virtual ICollection&lt;_EntityUser&gt; Users { get; set; }
  
      public virtual ICollection&lt;_EntityUserPermission&gt; UserPermissions { get; set; }
  
      internal _EntityUserRole()
      {
          Users = new List&lt;_EntityUser&gt;();
          UserPermissions = new List&lt;_EntityUserPermission&gt;();
      }
  }
  
  public class UserPermission : BaseEntity
  {
      public string Name { get; set; }
  
      public virtual ICollection&lt;_EntityUserRole&gt; UserRoles { get; set; }
  
      internal _EntityUserPermission()
      {
          UserRoles = new List&lt;_EntityUserRole&gt;();
      }
  }
  </code></pre>
  
  <p><strong>Mapping.cs</strong></p>
  
  <pre><code>public abstract class BaseEntityMap&lt;T&gt; : EntityTypeConfiguration&lt;T&gt; where T : BaseEntity
  {
      public BaseEntityMap()
      {
          ToTable(typeof(T).Name);
          HasKey(a =&gt; a.Id);
      }
  }
  
  public class UserMap : BaseEntityMap&lt;User&gt;
  {
      public UserMap()
          : base()
      {
          HasRequired(x =&gt; x.UserRole)
              .WithMany(x =&gt; x.Users)
              .HasForeignKey(x =&gt; x.UserRoleId);
      }
  }
  
  public class UserRoleMap : BaseEntityMap&lt;UserRole&gt;
  {
      public UserRoleMap()
          : base()
      {
          HasMany(x =&gt; x.UserPermissions)
              .WithMany(x =&gt; x.UserRoles)
              .Map(x =&gt;
              {
                  x.MapLeftKey("UserRoleId");
                  x.MapRightKey("UserPermissionId");
                  x.ToTable("_EntityUserRole_UserPermission");
              });
      }
  }
  
  public class UserPermissionMap : BaseEntityMap&lt;UserPermission&gt;
  {
      public UserPermissionMap()
          : base()
      {
      }
  }
  </code></pre>
  
  <p><strong>SqlServerDataContext.cs</strong></p>
  
  <pre><code>internal class SqlServerDataContext : DbContext
  {
      public DbSet&lt;User&gt; Users { get; set; }
  
      public DbSet&lt;UserRole&gt; UserRoles { get; set; }
  
      public DbSet&lt;UserPermission&gt; UserPermissions { get; set; }
  
      public SqlServerDataContext()
          : base("Name=MyDB")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Configurations.Add(new UserMap());
          modelBuilder.Configurations.Add(new UserRoleMap());
          modelBuilder.Configurations.Add(new UserPermissionMap());
  
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>    private static void Main()
      {
          using (SqlServerDataContext ctx = new SqlServerDataContext())
          {
              UserRole role = new UserRole();
              role.Name = "Super";
  
              UserPermission prm1 = new UserPermission();
              prm1.Name = "Delete";
              role.UserPermissions.Add(prm1);
  
              UserPermission prm2 = new UserPermission();
              prm2.Name = "Add";
              role.UserPermissions.Add(prm2);
  
              UserPermission prm3 = new UserPermission();
              prm3.Name = "Edit";
              role.UserPermissions.Add(prm3);
  
              User user = new User();
              user.Name = "User 1";
              user.UserRole = role;
  
              ctx.Users.Add(user);
              ctx.SaveChanges();
          }
  
          using (SqlServerDataContext ctx = new SqlServerDataContext())
          {
              var b = ctx.Users.Find(1);
          }
      }
  </code></pre>
  
  <p><strong>Result</strong>
  <img src="https://i.stack.imgur.com/yWuN4.jpg" alt="user">
  <img src="https://i.stack.imgur.com/t4ENC.jpg" alt="retrieve"></p>
  
  <p>As in print screen, after i insert a record into <strong>User</strong> and when i retrieve back the <strong>UserPermissions</strong> is empty. Please help! Thanks.</p>