title: Entity Framework: I set the foreign key, SaveChanges then access the navigation property, but it doesn't load the related entity. Why not?
body: <p>I am using this Entity class with Entity Framework 5 Code First:</p>
  
  <pre><code>public class Survey
  {
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int ID { get; set; }
  
      public string SurveyName { get; set; }
  
      [Required]
      public int ClientID { get; set; }
  
      [ForeignKey("ClientID")]
      public virtual Client Client { get; set; }
  }
  </code></pre>
  
  <p>And in my Controller's Create method I do this:</p>
  
  <pre><code>    Survey entity = new Survey()
      {
          SurveyName = "Test Name",
          ClientID = 4
      };
      db.Surveys.Add(entity);
      db.SaveChanges();
      Client c1 = entity.Client;                    //Why is this null?
      Client c2 = db.Clients.Find(entity.ClientID); //But this isn't?
  
      string s2 = c2.ClientName;
      string s1 = c1.ClientName;   //null reference thrown here
  </code></pre>
  
  <p>The Client navigation property remains null after SaveChanges. I expected the call to load the Client from the database because the foreign key exists. Why didn't it do that?</p>
  
  <p><strong>EDIT</strong>
  The code here comes from when my controllers were dependent on <code>DbContext</code>. Not long after I got this working I re-factored the code to use repositories and a unit of work. Part of that move was driven by the fact that it just felt wrong to use <code>Create</code> when I wanted to use <code>new</code>. What happened then was that I hit a problem with <a href="https://stackoverflow.com/questions/16736143/how-to-ensure-proxies-are-created-when-using-the-repository-pattern-with-entity">how to ensure proxies are created when using the repository pattern</a>. </p>