Unable to case object of type ProfiledDbDataReader to type SqlDataReader

I’ve encountered a similar problem to the one raised here: http://code.google.com/p/mvc-mini-profiler/issues/detail?id=125 that was unable to be reproduced re compiled linq2sql queries.

System.InvalidCastException: Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbDataReader' to type 'System.Data.SqlClient.SqlDataReader'.

The specific scenario which seems to cause this for me:

  1. run ASP.NET MVC solution with the profiling disabled - I have this as an option against a user account so different users can toggle on/off the profiling. When disabled, a normal SqlConnection is used.
  2. hit the page that uses the compiled query - all OK
  3. enable the profiling functionality (toggle the user account level flag) so that db connections are now wrapped in a ProfiledDbConnection
  4. revisit the page that uses the compiled query = error

I am not sure this can be fixed, my gut reaction here would be recommending you don’t run compiled queries while profiling.