18

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

Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210

3 Answers3

8

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

        }
Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210
  • 4
    the whole point is that it is safe to use in production, there is not need to do the #if DEBUG stuff ... if a session is not profiling Get will return the original connection – Sam Saffron Jun 09 '11 at 23:30
  • i m having strange problem when using mini profiler with linq to sql plz have a look at http://stackoverflow.com/questions/6410756/using-different-overload-of-datacontext-in-linq-to-sql – Muhammad Adeel Zahid Jun 21 '11 at 06:09
  • Where do you put this method and how are you calling it? – RyanW Aug 26 '11 at 15:53
  • @RyanW: I have a wrapper class that creates my datacontext. That's where this method is located. – Adrian Grigore Aug 29 '11 at 12:09
  • Do you need to do something to close this connection, or will it close itself when the context is disposed? – Aaron D Jan 26 '12 at 01:03
  • Where do I put this? IN my dbml? or context.cs? Do I make a new function or is there supposed to be CreteNewContext somewhere in those files? I am a bit lost with this – Piotr Kula Feb 07 '13 at 10:07
  • @ppumkin: I put this in a wrapper class around my datacontext. I needed a wrapper anyway for various other reasons. – Adrian Grigore Feb 08 '13 at 11:17
6

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);
}
normanthesquid
  • 690
  • 1
  • 6
  • 21
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.

CassOnMars
  • 6,153
  • 2
  • 32
  • 47