title: Entity Framework Populating Child Objects using Foreign Key
body: <p>I hope this isn't a duplicate as I have been looking but I was looking more at the reasoning behind this. </p>
  
  <p>I have set up a user object. </p>
  
  <pre><code>public class User
  {
      public User()
      {
  
      }
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int UserID { get; set; }
      public Guid UserGuid { get; set; }
      public string Email { get; set; }
      public string Name { get; set; }
      public int CompanyID { get; set; }
      public int StatusID { get; set; }
      [ForeignKey("StatusID")]
      public Status Status { get; set; }
      public int RoleID { get; set; }
      [ForeignKey("RoleID")]
      public UserRole UserRole { get; set; }
  }
  </code></pre>
  
  <p>And the Child Objects</p>
  
  <pre><code>public class UserRole
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int RoleId { get; set; }
      public string Role { get; set; }
  }
  
  public class Status
  {
      [Key]
      public int StatusId { get; set; }
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>When I call </p>
  
  <pre><code>var testuser = dbContext.User.Where(u =&gt; u.CompanyID == 1).FirstOrDefault();
  </code></pre>
  
  <p>I get users.Status as being null, but users.StatusID = 1. </p>
  
  <p>however, if I call</p>
  
  <pre><code>var status = dbContext.Status.ToList();
  var role = dbContext.UserRole.ToList();
  var testuser = dbContext.User.Where(u =&gt; u.CompanyID == 1).FirstOrDefault();
  </code></pre>
  
  <p>then call testuser.Status, I can see the correct object related to StatusId of 1. </p>
  
  <p>Can someone explain why this would solve the issue and also how I can make it work without having to call the following beforehand.</p>
  
  <pre><code>var status = dbContext.Status.ToList();
  </code></pre>
  
  <p>Thanks</p>