How can I make the ASP.NET MVC mini profiler work

2019-01-17 20:55发布

The ASP.NET MVC Mini Profiler looks awesome, but I don't get the Linq 2 SQL usage example.

This is the Linq2SQL example from the profiler documentation:

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

How do I use this in my actual application? I would have expected some kind of wrapper around my DataContext, but this seems to work in a different way. I don't even know where that that "GetConnection()" method from the example is defined.

Thanks,

Adrian

3条回答
手持菜刀,她持情操
2楼-- · 2019-01-17 21:34

Finally figured it out. In case someone else has the same question:

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

        }
查看更多
forever°为你锁心
3楼-- · 2019-01-17 21:36

None of the other answers worked for me. Adding this to my DataClassesDataContext Class in my DataClasses.Designer.cs did:

public static DataClassesDataContext CreateNewContext()
{
     var sqlConnection = new DataClassesDataContext().Connection;
     var profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(sqlConnection);
     return new DataClassesDataContext(profiledConnection);
}
查看更多
等我变得足够好
4楼-- · 2019-01-17 21:48

GetConnection() is a function that would return a DbConnection. You'll probably just do

var conn = ProfiledDbConnection.Get(new System.Data.SqlClient.SqlConnection(your_connection_string));

instead.

查看更多
登录 后发表回答