Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 441
Adrian Grigore Avatar asked Jun 09 '11 16:06

Adrian Grigore


3 Answers

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

        }
like image 135
Adrian Grigore Avatar answered Sep 22 '22 12:09

Adrian Grigore


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);
}
like image 39
normanthesquid Avatar answered Sep 19 '22 12:09

normanthesquid


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.

like image 32
CassOnMars Avatar answered Sep 20 '22 12:09

CassOnMars