How to Set up Miniprofiler with ASP .Net MVC EntityFramework 6 CodeFirst??

What is a Profiler?

Profiling helps in gathering information about an executing application, i.e how much time is spend where which in turn helps allowing to determine those improvements that are required in the application. Anything that helps us do profiling is called as a profiler.

Read more about why profilers and good .NET profilers.

One of them is Miniprofiler

MiniProfiler

Miniprofiler is developed by the famous team at StackOverflow. Clients using MiniProfiler are

  • Godaddy
  • StackOverflow

Steps

  • First of all we need to install two nuget packages MiniProfiler and MiniProfiler.EF6

  • In order to use the profiler we need to add the following to Global.asax file.

  • Then we need to add the following line in the SiteLayout.cshtml file just above the closing of the head.
  • Add the code that you want to profile. My case is HomeController and Index action.
  • Doing the above steps you will still not be able to see the Profiler output when you run the application.To check the profiler output add the following code to Web.Config file.
<system.data>
<DbProviderFactories>
<remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
<add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider"
description="MvcMiniProfiler.Data.ProfiledDbProvider"
type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.6.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>
</system.data>
  • This is all for the simple MiniProfiler.If you run your application you may see something like this.

MiniProfilerSuccess

  • Now comes the part of setting up with CodeFirst. For implementing this we have already installed MiniProfiler.EF6.We just need to add it to Application_Start().
  • Add the following code to Web.Config file.This will help in profiling for the queries that are executing.
<system.webServer>
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>

Thats it. Miniprofiler will be up and running.Yeah….. 🙂

Errors

The above steps will help you in setting up MiniProfiler. But when I set it up, our project had some dependencies and I was getting few errors while setting this up. In this section I will take you through what errors you are likely to get and how you can resolve it if your project has some extra dependencies like mine on other third party libraries.

Unable to define EFProfiledDbProviderServices class of type ‘ProfiledDbProviderServices’. Please check that your web.config defines a <DbProviderFactories> section underneath <system.data>
  • This error will come up when you have not done the last point.
  • You have correctly added the code as mentioned in last point but still get the error.Make sure that you don’t have HibernatingRhinos initialization on. It isn’t compatible with EntityFrameworkProfiler.Check here.

The next error that I was getting is almost same as the above one but a slightly different one related to Glimpse.It is such that in our project we had installed glimpse but we were not using it and then we decided to use this MiniProfiler.The error is

Unable to define EFProfiledDbProviderServices class of type ‘GlimpseDbProviderServices’. Please check that your web.config defines a <DbProviderFactories> section underneath <system.data>

You get this error when you have Glimpse installed in your project. Just make sure you uninstall Glimpse. If you still see this error coming then make sure you get to the bin folder of the application and delete Glimpse related files. Check here.

Read more about the implementation from here and here.

Thanks for reading the post. Comments and suggestions are welcome 🙂.