如何在不更改源代码的情况下将MVCMiniProfiler与PetaPoco集成 [英] How can integrate MVCMiniProfiler with PetaPoco without changing the sources

查看:117
本文介绍了如何在不更改源代码的情况下将MVCMiniProfiler与PetaPoco集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 MVCMiniProfiler PetaPoco

我正在尝试在创建PetaPoco数据库时设置连接,但是遇到了问题(connectionClosed)

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;
    }

}

我已阅读此项目 https://github.com/toptensoftware/PetaPoco/issues/44 ,但可以正常使用

I've read this item https://github.com/toptensoftware/PetaPoco/issues/44 but can get it to work

正确的方法是什么?

该解决方案由Gareth Elms提供:

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);
    }
}

推荐答案

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

I wonder if this is because it is a static class. It might be some weirdness around the connection being automatically closed after a request, and petapoco's _sharedConnectionDepth counter not knowing about it. I reproduced this easily using your code. Have a look at my sample petapoco app at github https://github.com/GarethElms/PetaPoco----A-simple-web-app all I do is instantiate Database in the base controller

这篇关于如何在不更改源代码的情况下将MVCMiniProfiler与PetaPoco集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆