1

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...".

Peter Meinl
  • 2,566
  • 25
  • 39
  • For a more detailed discussion see [How to handle Self-Tracking Entities InvalidOperationException](http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/8ee96457-0182-4e6c-a925-d8c87b13be65) – Peter Meinl Oct 14 '12 at 12:35

1 Answers1

0

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

Peter Meinl
  • 2,566
  • 25
  • 39