MvcMiniProfiler 1.9 and LLBLGen Pro 3.5 SelfServicing stored procedures

Posts   
 
    
Posts: 15
Joined: 16-May-2012
# Posted on: 05-Feb-2013 09:44:01   

I am using MvcMiniProfiler 1.9 and LLBLGen Pro 3.5 SelfServicing with SqlServer 2012 and it works fine until I try to call a stored procedure. Then I get an error: Unable to cast object of type 'MvcMiniProfiler.Data.ProfiledDbCommand' to type 'System.Data.SqlClient.SqlCommand'.

Here is how I use it: 1. Created a custom DynamicQueryEngine:


public class ProfilingDynamicQueryEngine : DynamicQueryEngine
{
    protected override DbCommand CreateCommand()
    {
        var cmd = base.CreateCommand();
        return new ProfiledDbCommand(cmd, null, MiniProfiler.Current);
    }
}

  1. Created a partial CommonDaoBase class:

public partial class CommonDaoBase
{
    public CommonDaoBase(InheritanceHierarchyType typeOfInheritance, string entityName, IEntityFactory entityFactory)
        : base(InheritanceInfoProviderSingleton.GetInstance(), new ProfilingDynamicQueryEngine(), typeOfInheritance, entityName, entityFactory)
    {
    }

    public override DbConnection CreateConnection(string connectionString)
    {
        var dbConnection = base.CreateConnection(connectionString);
        return new ProfiledDbConnection(dbConnection, MiniProfiler.Current);
    }
}

  1. Called a stored procedure:

var dataTable = RetrievalProcedures.MyStoredProcedure();

Then I get an error in generated RetrievalProcedures file at this line:


DataTable toReturn = call.FillDataTable();

Error message is: Unable to cast object of type 'MvcMiniProfiler.Data.ProfiledDbCommand' to type 'System.Data.SqlClient.SqlCommand'.

Does anyone have any ideas why I am getting this error and how to fix it?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 05-Feb-2013 19:16:30   

Which LLBLGen Pro runtime library version (build no.) are you using?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Feb-2013 08:11:00   

I think I know what it is: SPs calls create DbDataAdapter from the DBFactory, which returns a System.Data.SqlClient.SqlDataAdapter, which expects a System.Data.SqlClient.SqlCommand to be set. So, when your custom DQE returns a MvcMiniProfiler.Data.ProfiledDbCommand, the cast exception is expected.

To make it work, you have to create a Profiled version of DataAdapter. So add this to your CommonDaoBase partial class:

public override System.Data.Common.DbDataAdapter CreateDataAdapter()
{
    var adapter = base.CreateDataAdapter();
    return new ProfiledDbDataAdapter(adapter, MiniProfiler.Current);
}
David Elizondo | LLBLGen Support Team
Posts: 15
Joined: 16-May-2012
# Posted on: 06-Feb-2013 11:15:18   

Thank you for your help. It works now.