views:

207

answers:

2

Should I bother doing this check to see if the object(s) saved correctly?

if (objectContext.SaveChanges() > 0)

It should just throw an exception if it didn't, right?

+2  A: 

According to the msdn documentation, the SaveChanges() method will return the number of records Added, Modified, or Deleted. The exceptions it could throw are InvalidOperationException or OptimisticConcurrencyException.

I think there is some value in checking the number of records. If you issue a valid statement that tries to modify or delete, but there's nothing to modify or delete, it's just going to return 0, even though there won't be an exception because technically nothing wrong happened.

Joseph
+2  A: 

According to MSDN:

SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.

So you will want to try/catch for the exception instead. If you want to know the number of affected records then in the try block you would have int recordsAffected = objectContext.SaveChanges();

Take a look at the MSDN link for an example.

Ahmad Mageed