Check if an insert or update was successful in Entity Framework

dev picture dev · Aug 25, 2009 · Viewed 13.9k times · Source

In ADO.NET, ExecuteNonQuery() "For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command" (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx)

In EF v1, context.SaveChanges() method returns "The number of objects in an Added, Modified, or Deleted state when SaveChanges was called." (http://msdn.microsoft.com/en-us/library/bb739065.aspx)

Please tell, when multiple entities (or single entity) are added or updated to context and context.SaveChanges() method is called, how to check if actual INSERT or UPDATE was successful.

Can we assume if there was NO exception that INSERT(s) or UPDATE(s) was successful ?

Thank You

Answer

Andrew Peters picture Andrew Peters · Aug 26, 2009

Yes, if there is no exception you may assume that the statements executed successfully.