title: Violation of UNIQUE KEY constraint on Update in ASP.net MVC
body: <p>Can someone please check out this code, i really dont understand why i got violation of unique when i try to update an record. the code used to create new record work just fine, but when i try to use it to update, it called out violation.</p>
  
  <p>Controller: </p>
  
  <pre><code>    [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Edit(User user)
      {
          if (ModelState.IsValid)
          {
              userRepository.SaveUser(user);
              return RedirectToAction("List");
          }
          else
              return View("Edit");
      }
  </code></pre>
  
  <p>userRepo:</p>
  
  <pre><code>    public void SaveUser(User user)
      {
          user.LAST_ACTIVITY = DateTime.Now;
          if (user.USER_ID != 0)
          {
              usersTable.Attach(user);
              usersTable.Context.Refresh(RefreshMode.KeepCurrentValues, user);
          }
          else
          {
              usersTable.InsertOnSubmit(user);
          }
          usersTable.Context.SubmitChanges();
      }
  </code></pre>
  
  <p>and i got an error:</p>
  
  <blockquote>
    <p>Unable to refresh the specified
    object.  The object no longer exists
    in the database.</p>
  </blockquote>
  
  <p>when i try to change the userRepo like this for testing purpose.</p>
  
  <pre><code>    public void SaveUser(User user)
      {
          user.LAST_ACTIVITY = DateTime.Now;
          usersTable.Attach(user);
          usersTable.Context.Refresh(RefreshMode.KeepCurrentValues, user);
          usersTable.Context.SubmitChanges();
      }
  </code></pre>
  
  <p>Im wondering if there anyone on this board can find out if i am wrong somewhere in this problem :).</p>
  
  <p>Thank you very much and wish you best regard. :)</p>