我是否可以与 MVC Mini Profiler 建立多个配置文件连接?

发布于 2024-12-15 21:28:32 字数 69 浏览 2 评论 0原文

我想在我的 MVC3 应用程序中混合使用 Dapper 和 EF 4。是否可以拥有多个配置文件连接,或共享一个配置文件连接?

I'd like to use a mixture of both Dapper and EF 4 in my MVC3 application. Is it possible to have more than one profiled connection, or share a profiled connection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-12-22 21:28:32

是的,您可以使用 EF 配置文件连接与 Dapper,这就是我最终使用的。

    private static DatabaseContext GetDatabaseContext()
    {
        using (MiniProfiler.Current.Step("Create Oracle EF Context"))
        {
            var pConn = ProfiledConnection;
            return pConn.CreateObjectContext<DatabaseContext>();
        }
    }

    private static EFProfiledDbConnection ProfiledConnection
    {
        get
        {
            using (MiniProfiler.Current.Step("Create Oracle Profiled Connection"))
            {
                var connectionString =
                    ConfigurationManager.ConnectionStrings["DatabaseContext"].ConnectionString;
                var ecsb = new EntityConnectionStringBuilder(connectionString);
                var oraConn = new OracleConnection(ecsb.ProviderConnectionString);
                var pConn = new EFProfiledDbConnection(oraConn, MiniProfiler.Current);
                return pConn;
            }
        }
    }

然后我以这种方式使用

            using (var ctx = GetDatabaseContext())
            {
                var result = ctx.SOME_EF_ENTITY.FirstOrDefault(c => c.LOGINNAME == username && c.PASSWORD == password);


                return result;
            }

它们

        const string sql = @"
                    SELECT ppro_status_code_id 
                    FROM   ppro_status_codes 
                    WHERE  table_name = 'CLIENT_FC_SHARED' 
                        AND status_code = :status_code";

        using (var cnn = ProfiledConnection)
        {
            cnn.Open();

            var data = cnn.Query<dynamic>(sql, new { status_code = code });
            var result = data.First();
            var codeID = result.PPRO_STATUS_CODE_ID;
            return codeID;
        }

Yes, you can use the EF profiled connection with Dapper, here's what I ended up using.

    private static DatabaseContext GetDatabaseContext()
    {
        using (MiniProfiler.Current.Step("Create Oracle EF Context"))
        {
            var pConn = ProfiledConnection;
            return pConn.CreateObjectContext<DatabaseContext>();
        }
    }

    private static EFProfiledDbConnection ProfiledConnection
    {
        get
        {
            using (MiniProfiler.Current.Step("Create Oracle Profiled Connection"))
            {
                var connectionString =
                    ConfigurationManager.ConnectionStrings["DatabaseContext"].ConnectionString;
                var ecsb = new EntityConnectionStringBuilder(connectionString);
                var oraConn = new OracleConnection(ecsb.ProviderConnectionString);
                var pConn = new EFProfiledDbConnection(oraConn, MiniProfiler.Current);
                return pConn;
            }
        }
    }

Then I used them in this way

            using (var ctx = GetDatabaseContext())
            {
                var result = ctx.SOME_EF_ENTITY.FirstOrDefault(c => c.LOGINNAME == username && c.PASSWORD == password);


                return result;
            }

and

        const string sql = @"
                    SELECT ppro_status_code_id 
                    FROM   ppro_status_codes 
                    WHERE  table_name = 'CLIENT_FC_SHARED' 
                        AND status_code = :status_code";

        using (var cnn = ProfiledConnection)
        {
            cnn.Open();

            var data = cnn.Query<dynamic>(sql, new { status_code = code });
            var result = data.First();
            var codeID = result.PPRO_STATUS_CODE_ID;
            return codeID;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文