如何使ASP.NET MVC迷你探查器与Linq 2 SQL一起使用?

Adr*_*ore 18 asp.net-mvc linq-to-sql mvc-mini-profiler

ASP.NET MVC迷你探查看起来真棒,但我不明白的LINQ的2 SQL使用示例.

这是profiler文档中的Linq2SQL示例:

partial class DBContext
{
   public static DBContext Get()
   {
      var conn = ProfiledDbConnection.Get(GetConnection());
      return new DBContext(conn);
      // or: return DataContextUtils.CreateDataContext<DBContext>(conn);
   }
}
Run Code Online (Sandbox Code Playgroud)

我如何在实际应用中使用它?我本来期望我的DataContext有一些包装器,但这似乎以不同的方式工作.我甚至不知道定义了示例中的"GetConnection()"方法的位置.

谢谢,

阿德里安

Adr*_*ore 7

终于想通了.如果其他人有同样的问题:

 private static DataClassesDataContext CreateNewContext()
        {
            var sqlConnection = new SqlConnection(<myconnectionstring>);
            var profiledConnection = ProfiledDbConnection.Get(sqlConnection);
            return DataContextUtils.CreateDataContext<DataClassesDataContext>(profiledConnection);

        }
Run Code Online (Sandbox Code Playgroud)

  • 重点是它在生产中使用是安全的,不需要做#if DEBUG的东西...如果一个会话没有分析Get将返回原始连接 (4认同)

nor*_*uid 6

没有其他答案对我有用.将它添加到我的DataClasses.Designer.cs中的DataClassesDataContext类中:

public static DataClassesDataContext CreateNewContext()
{
     var sqlConnection = new DataClassesDataContext().Connection;
     var profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(sqlConnection);
     return new DataClassesDataContext(profiledConnection);
}
Run Code Online (Sandbox Code Playgroud)

  • 似乎对我不起作用,ProfiledDbConnection上没有Get()方法的定义. (3认同)
  • @RyanW`使用新的ProfiledDbConnection(sqlConnection,MiniProfiler.Current)`. (2认同)