So, you are executing some Entity Framework code that seems ok and you are getting constaint exception when calling Save()
Then you think. “I’m going to watch whats going on. I’ll check EXACTLY the Sql sentence that’s being executed and find the Bug”. And you are going to desperate.
Intellitrace will show you the Sql sentence. But not the parameters
And there is an IQueryable.ToTraceString() but you have to modify your code AND it’s intented yo use with Queries, and you want to see a modifying sentence.
The solution: SqlProfiler
- Open SqlProfiler
- New Trace
- Choose Standart
- Event Selection
- Uncheck Audit, Existing Connection (and Stored procedure if you want)
- Check Show all Filters
- In TSQL Check ONLY:
Why Starting and not any other event? Because if the sentence is failing (for example: a unique constraint) the “starting event” is the only one that records a trace, it will never complete.
Extra: when you have a query
In case you are executing a Query and you don’t have Sql Profiler, you can check this solution posted in stack overflow. I haven’t tried it myself but it looks fine. You have to modify your code, though