Question

STEs throw two types of Exception:

  • UpdateException
  • InvalidOperationException

With UpdateException we can check the inner SQLExeption.Number to identify the the Exception (ex. 515=NullNotAllowed)

InvalidOperationException seems not to expose a code. The Hresult property is protected.

Currently I am parsing InvalidOperationException.Message which is ugly:

Try
    Using ctx AS New MyEntities
        ctx.Orders.ApplyChanges(order)
        ctx.SaveChanges()
    End Using
Catch ex As InvalidOperationException When ex.Message.Contains("...foreign-key properties is non-nullable")
    Throw New FaultException("...")
Catch ex As UpdateException When CType(ex.InnerException, SqlException).Number = SQLErrorNumbers.NullNotAllowed
    Throw New FaultException("...")
End Try

How are we supposed to differentiate InvalidOperationExceptions? Is there a list of possible InvalidOperationExceptions? Can one access the protected HResult?

EDIT No I am not talking about the "duplicate entries" InvalidOperationExceptions! I am getting an InvalidOperationException "The relationship could not be changed because one or more of the foreign-key properties is non-nullable...".

Was it helpful?

Solution

There seems to be no way around the ugly exception handling with STE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top