I'm trying to get MVCMiniProfiler to work with PetaPoco
I'm trying to set the connection in the creation of the PetaPoco DB, but run into problems (connectionClosed)
public class DbHelper
{
static Database _CurrentDb = null;
public static Database CurrentDb()
{
if (_CurrentDb == null)
{
string connstr = ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;
var conn = ProfiledDbConnection.Get(new SqlConnection(connstr));
_CurrentDb = new PetaPoco.Database(conn);
}
return _CurrentDb;
}
}
I've read this item https://github.com/toptensoftware/PetaPoco/issues/44 but can get it to work
What is the right way to do it?
Edit
The solution was provided by Gareth Elms:
public class DbHelper
{
static Database _CurrentDb = null;
public static Database CurrentDb()
{
if (_CurrentDb == null)
{
_CurrentDb = new DatabaseWithMVCMiniProfiler("MainConnectionString");
}
return _CurrentDb;
}
}
public class DatabaseWithMVCMiniProfiler : PetaPoco.Database
{
public DatabaseWithMVCMiniProfiler(IDbConnection connection) : base(connection) { }
public DatabaseWithMVCMiniProfiler(string connectionStringName) : base(connectionStringName) { }
public DatabaseWithMVCMiniProfiler(string connectionString, string providerName) : base(connectionString, providerName) { }
public DatabaseWithMVCMiniProfiler(string connectionString, DbProviderFactory dbProviderFactory) : base(connectionString, dbProviderFactory) { }
public override IDbConnection OnConnectionOpened( IDbConnection connection)
{
// wrap the connection with a profiling connection that tracks timings
return MvcMiniProfiler.Data.ProfiledDbConnection.Get( connection as DbConnection, MiniProfiler.Current);
}
}