How can I make the ASP.NET MVC mini profiler work with Linq 2 SQL?

岁酱吖の 提交于 2019-11-27 02:09:16

问题


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


回答1:


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

        }



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/6296518/how-can-i-make-the-asp-net-mvc-mini-profiler-work-with-linq-2-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!