I have Entity Framework set up to update a table. The update is intercepted by an instead-of trigger, which calls RAISERROR:
CREATE TRIGGER mySchema.UpdateBusinessObjects
ON mySchema.BusinessObjects
INSTEAD OF UPDATE
AS
RAISERROR(''test error from SQL'',16,1)
RETURN
In my repository class, I'm attempting to catch the SqlException generated by the RAISERROR in SQL:
public void SaveBusinessObject(BusinessObject b) {
try {
repo.Entry(b).State = EntityState.Modified;
repo.SaveChanges();
} catch (SqlException ex) {
// handle exception here
}
}
The problem is that C# isn't catching the SqlException; it gets passed up to the caller as an unhandled exception ("SqlException was unhandled by user: test error from SQL"). What?!
It looks like EF's SaveChanges() somehow is passing the exception up over my try catch block. I have tried switching my catch statement to catch (Exception ex) in case the EF exception is somehow more general, but I still get an unhandled SqlException. Am I missing something simple here? What's the problem with the SaveChanges() method?