Errors occasionally occur when saving data using the FlightlnfoContext ADO.NET Entity Framework context.
Updates to the data are being lost when an error occurs. You need to ensure that data is still saved when an
error occurs by retrying the operation. No more than five retries should be performed. With which codesegment
should you replace the body of the SaveChanges() method in the FlightInfoContext.cs file?
A.
Option A
B.
Option B
C.
Option C
D.
Option D
Explanation:
EntitySqlException : Represents errors that occur when parsing Entity SQL command text. This exceptionis
thrown when syntactic or semantic rules are violated.
SqlException : The exception that is thrown when SQL Server returns a warning or error. This class cannot be
inherited.
EntityCommandExecutionException : Represents errorsthat occur when the underlying storage provider could
not execute the specified command. This exception usually wraps a provider-specific exception.
This doesn’t make sense. Option A is nonsense, as is Option B. Option D is semantically nonsense, which leaves option C.
However. Entity Framework does not throw SqlException from DbContext.SaveChanges. Furthermore, if you look at the logic the whole IsTransient() stuff is moot because if the result of that method is true (transient) execution will continue with a retry. If the result if false (not transient) execution will be exactly the same as the loop reiterates.
So, in point of fact, all options are wrong!
DbContext.SaveChanges can definitely propagate the SqlException to be caught. However, your point is still valid – even if IsTransient returns false, the exception is swallowed in the catch block, and the loop iterates regardless.