Entity Framework Profiler and Sitecore

While working with Entity Framework on a Sitecore project I ran into some EF issues that required the use of Entity Framework Profiler by Hibernating Rhinos. The setup Instructions require you to add an initializer to the Application_Start, obviously this is not a viable approach with Sitecore. After doing some research a Hook seemed like it would be the appropriate means of initializing the profiler. Unfortunately after adding the following class and config, the EFProfiler wasn't being initialized. Hooks appear to be running too late during the application startup cycle.

namespace RunningWild.Sc.Hooks
{
    public class EFProfilerHook : IHook
    {
         public void Initialize()
         {   
           HibernatingRhinos.Profiler.Appender.EntityFramework
           .EntityFrameworkProfiler.Initialize();
        }
    }
}

```

````xml
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <hooks>
     <hook type="RunningWild.Sc.Hooks.EFProfilerHook, RunningWild.Sc"/>
    </hooks>
  </sitecore>
</configuration>
```

###Solution

The initialize pipeline is where I ultimately had success in getting the profiler to initialize early enough in the application startup cycle. What was the most interesting finding from this was that hooks are one of the first things executed in the initalize pipeline. After some more trial and error, I determined that the profiler needed to be the first thing to run in the initalize pipeline.  

```xml
<pipelines>
      <initialize>
        <processor type="Sitecore.Pipelines.Loader.ShowVersion, Sitecore.Kernel">
          <assemblies hint="list:AddAssembly">
            <assembly>/bin/Sitecore.Client.dll</assembly>
            <assembly>/bin/Sitecore.Kernel.dll</assembly>
            <assembly>/bin/Sitecore.Nexus.dll</assembly>
          </assemblies>
          <showVersionForAllAssemblies>true</showVersionForAllAssemblies>
          <showDatabases>true</showDatabases>
          <showDomains>true</showDomains>
          <showDebugWarning>true</showDebugWarning>
        </processor>
        <processor type="Sitecore.Pipelines.Loader.ShowHistory, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.Loader.SetGlobals, Sitecore.Kernel" />
        <processor type="Sitecore.Eventing.Remote.RemoteEventMap, Sitecore.Kernel"                                                    
         method="InitializeFromPipeline" />
        <processor type="Sitecore.Pipelines.Loader.LoadHooks, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.Loader.InitializeManagers, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.Loader.InitializeScheduler, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.Loader.InitializeHeartbeat, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.Loader.InitializeAgilityPack, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.Loader.EnsureAnonymousUsers, Sitecore.Kernel" />
      </initialize>
```

So the final solution ended up looking like the following:

```c#
namespace RunningWild.Sc.Pipelines.Initialize
{
    public class EFProfilerInitializer
    {
        public void Process(PipelineArgs args)
        {
            HibernatingRhinos.Profiler.Appender.EntityFramework
            .EntityFrameworkProfiler.Initialize();
        }
    }
}

```

```xml
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor patch:before="processor[@type='Sitecore.Pipelines.Loader.ShowVersion,  
         Sitecore.Kernel']" type="RunningWild.Sc.Pipelines.Initialize.EFProfilerInitializer,            
         RunningWild.Sc"/>
	    </initialize>
    </pipelines>
  </sitecore>
</configuration>
```