none
How to handle failed update? RRS feed

  • Question

  • Hi,

    What is the best way to handle a failed update? I currently do the following to remove the 'failed' version of the object from the Context - am I doing the right thing or is there a better way?

    James :-)

         public void Update(Teacher teacher)
        {
          try
          {
            this.GetDbSet<Teacher>().Attach(teacher);
            this.SetEntityState(teacher, EntityState.Modified);
            this.UnitOfWork.SaveChanges();
          }
          catch (System.Data.Entity.Infrastructure.DbUpdateException)
          {
            this.SetEntityState(teacher, EntityState.Detached);
            throw;
          }
          catch
          {
            throw;
          }
    
        }
    

     

    Tuesday, August 30, 2011 8:21 AM

Answers

  • Hi m0gb0y74,

    Welcome!

    When you call SaveChanges method, you can see the update T-SQL has been ran in SQL Server Profiler, but there is "0 rows affected".  Based on my opinion, I think your catch code is good way to handle the failed update.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by m0gb0y74 Thursday, September 1, 2011 9:19 AM
    Wednesday, August 31, 2011 8:59 AM
    Moderator

All replies

  • Hi m0gb0y74,

    Welcome!

    When you call SaveChanges method, you can see the update T-SQL has been ran in SQL Server Profiler, but there is "0 rows affected".  Based on my opinion, I think your catch code is good way to handle the failed update.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by m0gb0y74 Thursday, September 1, 2011 9:19 AM
    Wednesday, August 31, 2011 8:59 AM
    Moderator
  • Thanks for your help

     

    James :-)

    Thursday, September 1, 2011 9:20 AM