title: EF4/WCF SaveChanges() Best Practice
body: <p>This is how we implement a generic Save() service in WCF for our EF entities. A TT does the work for us. Even though we don't have any problems with it, I hate to assume this is the best approach (even if it might be). You guys seem pretty darn bright and helpful, so I thought I would pose the question:</p>
  
  <p>Is there a better way?</p>
  
  <pre><code>[OperationContract]
  public User SaveUser(User entity)
  {
      bool _IsDeleted = false;
      using (DatabaseEntities _Context = new DatabaseEntities())
      {
          switch (entity.ChangeTracker.State)
          {
              case ObjectState.Deleted:
                  //delete
                  _IsDeleted = true;
                  _Context.Users.Attach(entity);
                  _Context.DeleteObject(entity);
                  break;
              default:
                  //everything else
                  _Context.Users.ApplyChanges(entity);
                  break;
          }
          // now, to the database
          try
          {
              // try to save changes, which may cause a conflict.
              _Context.SaveChanges(System.Data.Objects.SaveOptions.None);
          }
          catch (System.Data.OptimisticConcurrencyException)
          {
              // resolve the concurrency conflict by refreshing 
              _Context.Refresh(System.Data.Objects.RefreshMode.ClientWins, entity);
              // Save changes.
              _Context.SaveChanges();
          }
      }
      // return
      if (_IsDeleted)
          return null;
      entity.AcceptChanges();
      return entity;
  }
  </code></pre>