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

时间:2011-11-15 18:30:05

标签: asp.net-mvc-3 entity-framework-4 mvc-mini-profiler dapper

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

1 个答案:

答案 0 :(得分:1)

是的,你可以使用与Dapper的EF配置连接,这是我最终使用的。

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