Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:30:27+00:00 2026-05-24T23:30:27+00:00

I’m sure it’s possible to profile the Enterprise Library SQL commands, but I haven’t

  • 0

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’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:30:27+00:00Added an answer on May 24, 2026 at 11:30 pm

    The exception comes from this line in Entlib Database.DoLoadDataSet

        ((IDbDataAdapter) adapter).SelectCommand = command; 
    

    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:

    1. Create two new classes ProfiledDbProviderFactoryForEntLib and DbDataAdapterForEntLib

      public class ProfiledDbProviderFactoryForEntLib : ProfiledDbProviderFactory
      {
          private DbProviderFactory _tail;
          public static ProfiledDbProviderFactory Instance = new ProfiledDbProviderFactoryForEntLib();
      
          public ProfiledDbProviderFactoryForEntLib(): base(null, null)
          {
          }
      
          public void InitProfiledDbProviderFactory(IDbProfiler profiler, DbProviderFactory tail)
          {
              base.InitProfiledDbProviderFactory(profiler, tail);
              _tail = tail;
          }
      
          public override DbDataAdapter CreateDataAdapter()
          {
              return new DbDataAdapterForEntLib(base.CreateDataAdapter());
          }
      
          public override DbCommand CreateCommand()
          {
              return _tail.CreateCommand(); 
          }        
      }
      
      public class DbDataAdapterForEntLib : DbDataAdapter
      {
          private DbDataAdapter _dbDataAdapter;
          public DbDataAdapterForEntLib(DbDataAdapter adapter)
          : base(adapter)
         {
              _dbDataAdapter = adapter;
         }
      }
      
    2. In Web.config, Add the ProfiledDbProviderFactoryForEntLib to DbProviderFactories and set ProfiledDbProviderFactoryForEntLib as providerName for your connectionstring

      <configuration>
          <configSections>
              <section name="dataConfiguration" type="..."  />
          </configSections>
          <connectionStrings>
              <add name="SqlServerConnectionString" connectionString="Data Source=xyz;Initial Catalog=dbname;User ID=u;Password=p"
        providerName="ProfiledDbProviderFactoryForEntLib" />
          </connectionStrings>
          <system.data>
              <DbProviderFactories>
                <add name="EntLib DB Provider"
                 invariant="ProfiledDbProviderFactoryForEntLib"
                 description="Profiled DB provider for EntLib"
                 type="MvcApplicationEntlib.ProfiledDbProviderFactoryForEntLib,     MvcApplicationEntlib, Version=1.0.0.0, Culture=neutral"/>
              </DbProviderFactories>
          </system.data>
          <dataConfiguration defaultDatabase="..." />
          <appSettings>... <system.web>... etc ...
      </configuration>
      

      (MvcApplicationEntlib is the name of my test project)

    3. Set up the ProfiledDbProviderFactoryForEntLib before any calls to the DB (readers sensitive to hacks be warned, this is where it gets ugly)

      //In Global.asax.cs 
          protected void Application_Start()
          {
      
              ProfiledDbProviderFactoryForEntLib profiledProfiledDbProviderFactoryFor = ((ProfiledDbProviderFactoryForEntLib)DbProviderFactories.GetFactory("ProfiledDbProviderFactoryForEntLib"));
              DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); //or whatever predefined factory you want to profile
              profiledProfiledDbProviderFactoryFor.InitProfiledDbProviderFactory(MiniProfiler.Current, factory); 
          ...
      

      This could probably been done in a better way or in another place. MiniProfiler.Current will be null here because nothing is profiled here.

    4. Call the stored procedure just as you did from the beginning

      public class HomeController : Controller
          {
              public ActionResult Index()
              { 
                  Database db = DatabaseFactory.CreateDatabase();
                  DbCommand dbCommand = db.GetStoredProcCommand("spGetSomething");
                  DbCommand cmd = new ProfiledDbCommand(dbCommand, dbCommand.Connection, MiniProfiler.Current);
                  DataSet ds = db.ExecuteDataSet(cmd);
                  ...
      

    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.

    1. 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.

      ProfiledDbProviderFactoryForEntLib profiledProfiledDbProviderFactoryFor = ((ProfiledDbProviderFactoryForEntLib) DbProviderFactories.GetFactory("ProfiledDbProviderFactoryForEntLib"));
      DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
      profiledProfiledDbProviderFactoryFor.InitProfiledDbProviderFactory(MvcMiniProfiler.MiniProfiler.Start(), factory);
      
    2. Remove the method CreateCommand from ProfiledDbProviderFactoryForEntLib to let the ProfiledDbProviderFactory create the profiled command instead.

    3. Execute your SP without creating a ProfiledDbCommand, like this

      Database db = DatabaseFactory.CreateDatabase();
      DbCommand dbCommand = db.GetStoredProcCommand("spGetSomething");
      DataSet ds = db.ExecuteDataSet(dbCommand);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.