I’m sure it’s possible to profile the Enterprise Library SQL commands, but I haven’t been able to figure out how to wrap the connection. This is what I have come up with:
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetStoredProcCommand(PROC);
ProfiledDbCommand cmd = new ProfiledDbCommand(dbCommand, dbCommand.Connection, MvcMiniProfiler.MiniProfiler.Current);
db.AddInParameter(cmd, "foo", DbType.Int64, 0);
DataSet ds = db.ExecuteDataSet(cmd);
This results in the following exception:
Unable to cast object of type ‘MvcMiniProfiler.Data.ProfiledDbCommand’ to type ‘System.Data.SqlClient.SqlCommand’.
The exception comes from this line in Entlib Database.DoLoadDataSet
In this case the adapter is of type SqlDataAdapter and it expects a SqlCommand, the command that is created by the ProfiledDbProviderFactory is of type ProfiledDbCommand as you see in the exception.
This solution will provide EntLib with a generic DbDataAdapter by overriding CreateDataAdapter and CreateCommand in the ProfiledDbProviderFactory.
It seems to work as it should but I apologize if I’ve overseen any unwanted consequenses this hack might have (or sore eyes it might have caused 😉 .
Here it goes:
Create two new classes ProfiledDbProviderFactoryForEntLib and DbDataAdapterForEntLib
In Web.config, Add the ProfiledDbProviderFactoryForEntLib to DbProviderFactories and set ProfiledDbProviderFactoryForEntLib as providerName for your connectionstring
(MvcApplicationEntlib is the name of my test project)
Set up the ProfiledDbProviderFactoryForEntLib before any calls to the DB (readers sensitive to hacks be warned, this is where it gets ugly)
This could probably been done in a better way or in another place. MiniProfiler.Current will be null here because nothing is profiled here.
Call the stored procedure just as you did from the beginning
Edit:
Ok wasn’t sure exactly how you wanted to use it. To skip the manual creation of a ProfiledDbCommand. The ProfiledDbProviderFactory needs to be initiated with the miniprofiler for every request.
In Global.asax.cs, Remove the changes you made to Application_Start (the factory setup in step 3 above), add this to Application_BeginRequest instead.
Remove the method CreateCommand from ProfiledDbProviderFactoryForEntLib to let the ProfiledDbProviderFactory create the profiled command instead.
Execute your SP without creating a ProfiledDbCommand, like this