views:

111

answers:

2

My problem is that when I get a SQL exception from the entity framework (say for example, because of a null value that isn't allowed to be null), it's sometimes hard to figure out which property is being referred to, and why it is null.

Now I know I can set up a SQL trace and log it which will give me the information I need, but this means I have to re-create the problem once I have turned logging on, which isn't always simple.

Ideally, I would like to be able to have the entity framework automatically include the actual SQL statement that caused the problem when it throws a SQL exception.

Is this possible?

A: 

if it is sql exception being thrown, then it will contain the detsil with it make sure that none of the exception is eaten away by throw new ex; or throw ex;, In catch use throw instead this will not suppress any part of the exception and you will have the detailed exception. this is the problem once I had hope it helps.

Vinay Pandey
No, that's not the problem, sorry. I'm not re throwing the exception at all. It's coming straight from the Entity framework layer. It is a SQLException and does not contain the SQL command that caused the problem.
Simon P Stevens
+1  A: 

Have you looked in the Errors property of the SqlException? It will contains a collection of SqlErrors that have the property Procedure that will state the name of the executed function that coused the error

zwi
The "Procedure" property on the SQLError is empty, but the "Message" property included the details I was after. Not sure how I managed to miss that before. Thanks very much. You've solved my actual problem (so I'll give you the answer), but my original query kind of still stands, there doesn't seem to be a way to get the actual query that the EF executed from the exception object. Anyway. Thanks again.
Simon P Stevens