title: Retrieve and update record with Entity Framework in web app
body: <p>I am new to development and trying to retrieve a record to display and edit in a FormView on an ASP.NET web app page, using Entity Framework 5.0 (database first method), but I am not sure of the best way to go about this.</p>
  
  <p>To retrieve the record, I use the following code:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      LoadData(int.Parse(Session["PersonID"].ToString()));
  }
  
  private void LoadData(int iPersonID)
  {
      using (PeopleEntities ctx = new PeopleEntities())
      {
          var query = (from a in ctx.People
                       where a.PersonID == iPersonID
                       select a).FirstOrDefault();
  
          TextBoxFirstName.Text = query.FirstName;
          TextBoxLastName.Text = query.LastName;
      }
  }
  </code></pre>
  
  <p>And to save it, I use:</p>
  
  <pre><code>protected void ButtonSave_Click(object sender, EventArgs e)
  {
      SaveEmployee(int.Parse(Session["PersonID"].ToString()));
  }
  
  private void SaveEmployee(int iPersonID = 0)
  {
      using (PeopleEntities ctx = new PeopleEntities())
      {  
          var query = (from a in ctx.People
                       where a.PersonID == iPersonID
                       select a).FirstOrDefault();
  
          query.FirstName = TextBoxFirstName.Text;
          query.LastName = TextBoxLastName.Text;
          ctx.SaveChanges();
      }
  }
  </code></pre>
  
  <p>It seems silly to me to have these two methods each query the database to retrieve and update the record, but, again, I am a novice, and maybe I'm just missing something.  Is there a way to populate the controls on the FormView with the entity and have a method to save the record without having to manually assign the values (query.FirstName = TextBoxFirstName.Text, etc.) based on state?</p>
  
  <p>I have seen the EntityDataSource, but I do not think that will be a good option for anything but the simplest of things.</p>
  
  <p>Can anyone please tell me if what I am doing is ok or provide a better example or guidance?</p>
  
  <p>Your help is greatly appreciated!</p>