我正在尝试让MVCMiniProfiler与PetaPoco一起工作
我正在尝试在创建 PetaPoco DB 时设置连接,但遇到问题(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;
}
}
我已经阅读了这个项目https://github.com/toptensoftware/PetaPoco/issues/44但可以让它工作
正确的方法是什么?
编辑
该解决方案由 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);
}
}