MVC Mini Profiler with Entity Framework and SQL Server CE 4.0

Recently I was trying to use MVC Mini Profiler released by Stack Overflow folks with my site on ASP.NET MVC 3 framework and had some problems to use its all features, because I had a little specific setup (more on that a bit later).

It was a piece of cake to use it for profiling usual code with:

using (MiniProfiler.Current.Step("Some profiling step description"))

Everything worked like a charm with just following a step-by-step guide on project’s site (http://code.google.com/p/mvc-mini-profiler/).

Problems started when I got to DB profiling support. I should point out that the specific situation I was in includes using SQL Server CE 4.0 with Entity Framework 4 with DB first approach.

As you can read from their instructions, you have to substitute their profiled connection for the real connection you usually use. But as it turned out not everything is so simple and after some experimenting and searching through Stack Overflow and generally Internet I arrived at the following solution to get Entity Framework’s ObjectContext for you DB Model.

public static T GetDb<T>(string connectionStringName) where T: ObjectContext
{
    var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
    var entityConnStr = new EntityConnectionStringBuilder(connectionString);
    var realConnection = new SqlCeConnection(entityConnStr.ProviderConnectionString);
    var profiledConnection = ProfiledDbConnection.Get(realConnection, MiniProfiler.Current);
    return profiledConnection.CreateObjectContext<T>();
}

Here connectionStringName is the name of connection string in your web.config (or app.config) file, T specifies the type of your ObjectContext.

So what do we do here is the following:

  1. We get connection string for web.config (or app.config). If you are using Entity Framework, this connection string will contain not only connection string per se, but also some metadata, which in my case was causing throwing exception by SqlCeConnection constructor.
  2. So we need to get rid of this metadata. We create EntityConnectionStringBuilder which parses nicely our long connection string and provides us with separate parts of it.
  3. For the bare connection to SQL Server CE 4.0 we need just a provider connection string part, which we extract with ProviderConnectionString property.
  4. After that we use ProfiledDbConnection.Get helper method provided by MVC Mini Profiler to create a profiled version of that connection, which we should use instead of standard one.
  5. We use another helper method (which is extension method and is contained within a static class ObjectContextUtils which is also provided by MVC Mini Profiler) to create our DB model object context.

And that’s all. Works very well for me. If you are using not CE edition of SQL Server, you have to change the SqlCeConnection class to SqlConnection.

I suppose you’ll create one more method for your specific application to not provide connection string name throughout all your application:

public static MyObjectContext GetDb() 
{
    return GetDb<MyObjectContext>("MyObjectContextConnStr");
}

Hope it helps not only for me next time you’ll try to use MVC Mini Profiler which is a really awesome project. Thanks to Stack Overflow guys for this thing! :)

Update (2011-08-02):

The original post was written while using MvcMiniProfiler 1.4. After updating today to version 1.7, DB profiling stopped working giving the following error message: “Unable to find the requested .Net Framework Data Provider. It may not be installed.” After quick googling, it appears that the following addition to web.config is enough to overcome this problem:

<system.data>
    <DbProviderFactories>
        <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
        <add description="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler" name="MvcMiniProfiler.Data.ProfiledDbProvider" />
    </DbProviderFactories>
</system.data>

We just register DB provider for MvcMiniProfiler and everything keeps working. :)

blog comments powered by Disqus

Notes

  1. anakryiko posted this