0

I'm having a discussion with a friend about how to check if the context was saved successfully in Entity Framework.

His opinion is that we should check the integer returned by SaveChanges() and compare it to what we expected
Example: Check expected with returned

Mine is that we can assume that SaveChanges() did it's job and everything was saved to the database if no exceptions are thrown.
Example: Catch exception

I found two similar question here, but they don't explain much. one two
So which method is better, and why?

Community
  • 1
  • 1
  • Why not use both? – Stefan Hegny Oct 04 '16 at 19:17
  • actually, when SaveChanges() does not throw an exception, the statement executed successfully. The other possibility is valid, however it is already used internally (inside SaveChanges()), which would cause a DbUpdateConcurrencyException or DbUpdateException (when ExecuteNonQuery, i.e. the to be return value of SaveChanges, is not what is expected), otherwise a DbEntityCommandException (or something along those lines) when the query could not be transmitted/there was a SqlException while executing. – DevilSuichiro Oct 04 '16 at 19:51

0 Answers0