4

我正在尝试让MVCMiniProfilerPetaPoco一起工作

我正在尝试在创建 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);
    }
}
4

2 回答 2

3

我想知道这是否是因为它是一个静态类。在请求后自动关闭连接可能有些奇怪,而 petapoco 的 _sharedConnectionDepth 计数器不知道它。我使用您的代码轻松地复制了这一点。看看我在 github 上的示例 petapoco 应用程序https://github.com/GarethElms/PetaPoco----A-simple-web-app我所做的就是在基本控制器中实例化数据库

于 2011-08-20T17:34:53.710 回答
1

正如 Schotime 提到的,每个请求对于 mvc 应用程序来说是最好的。这是我使用 Ninject 的 impl

private static void RegisterServices(IKernel kernel)
{


#if DEBUG
            kernel.Bind<IDatabase>().To<DebugDatabase>()
               .InRequestScope()
               .WithConstructorArgument("connectionStringName", "DebugCnnString");
#else
            kernel.Bind<IDatabase>().To<ReleaseDatabase>()
               .InRequestScope()
               .WithConstructorArgument("connectionStringName", "ReleaseCnnString");
#endif
}

public class DebugDatabase : PetaPoco.Database
{
    public DebugDatabase(string connectionStringName) : base(connectionStringName) { }

    public override IDbConnection OnConnectionOpened(IDbConnection connection)
    {
        // wrap the connection with a profiling connection
        return new ProfiledDbConnection(connection as DbConnection, MiniProfiler.Current);
    }
}

public class ReleaseDatabase : PetaPoco.Database
{
    public ReleaseDatabase(string connectionStringName) : base(connectionStringName)    {   }

    // ... some stuff
}
于 2012-02-15T09:34:40.247 回答