title: Weird behavior from Create() method in MVC4 - new object getting set with ID = 0, saved as something else
body: <p>I'm debugging this method for two cases: one where there is a parent, the other where there is no parent.  </p>
  
  <p>If there is no parent, the new Person has an id of 0 but never actually gets saved to the db.</p>
  
  <p>If there is a parent, the new Person has an id of 0 in this method, but a new record is inserted into the db with the correct value (one more than the highest in the table).  </p>
  
  <p>What is going on here?  I know I'm doing something wrong, I'm just not sure what.  </p>
  
  <p>I'm using EF Codefirst.  </p>
  
  <p>The code for the controller method: </p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(CreatePersonViewModel viewModel)
  {
      if (ModelState.IsValid)
      {
  
          var parent = _db.Persons.FirstOrDefault(s =&gt; s.PersonId == viewModel.ParentId);
  
          var person = new Person() { Name = viewModel.Name };
  
          // if it has a parent, build new relationship
          if (parent != null)
          {
              person.Parent = parent;
              parent.Children.Add(person);
          };
  
          _db.Save();
  
          return RedirectToAction("detail", "person", new { personId = person.PersonId });
      }
      return View(viewModel);
  }
  </code></pre>