4

Is it possible to use Mini-Profiler with Simple.Data Library? I use it to get data from MySql like this:

var db = Database.OpenConnection(ConnectionString);
var book = db.Books.FindById(id);

How can I user Profiler with this code?

casperOne
  • 73,706
  • 19
  • 184
  • 253
user1224129
  • 2,759
  • 3
  • 27
  • 29

2 Answers2

3

You can tell Simple.Data to use a pre-existing connections and wrap your connection with a profiled connection:

var db = Database.OpenConnection(ConnectionString);
using (var rawCnn =  new MySqlConnection(ConnectionString)) 
using (var profiledCnn = new MvcMiniProfiler.Data.ProfiledDbConnection(rawCnn, MiniProfiler.Current);
{
    profiledCnn.Open();
    ((AdoAdapter)db.GetAdapter()).UseSharedConnection(profiledCnn);
    book = db.Books.FindById(id);
    ((AdoAdapter)db.GetAdapter()).StopUsingSharedConnection();
}
Matt Warren
  • 10,279
  • 7
  • 48
  • 63
Mark Rendle
  • 9,274
  • 1
  • 32
  • 58
0

There is a new hook that was added to Simple.Data which allows for better integration with MiniProfiler.

AdoAdapter.ConnectionCreated += (o, args) => args.OverrideConnection(new ProfiledDbConnection((DbConnection)args.Connection, MiniProfiler.Current));

This basicallt allows you to hookup to the connection created event and override it with your own profiled connection.

NOTE: As of writing of this post, this change isn't in the nuget package yet. so you need your custom build of Simple.Data

kay.one
  • 7,622
  • 6
  • 55
  • 74