# Exception using EF6

**URL:** <https://community.miniprofiler.com/t/exception-using-ef6/253>\
**Category:** .NET\
**Created:** [April 8, 2014, 3:24pm UTC](https://community.miniprofiler.com/t/exception-using-ef6/253 "2014-04-08T15:24:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rolandomartinez](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@rolandomartinez](https://community.miniprofiler.com/u/rolandomartinez)\
**Post date:** [April 8, 2014, 3:24pm UTC](https://community.miniprofiler.com/t/exception-using-ef6/253/1 "2014-04-08T15:24:04Z")

</div>

Hi,  
I got MiniProfiler.EF6 3.0.10-beta4 package and at moment to run the app. I got this exception:

_“An exception of type ‘System.InvalidOperationException’ occurred in EntityFramework.dll but was not handled in user code. **Additional information:** The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an ‘Loaded’ event handler. ‘Loaded’ event handlers can only be added as part of application start up before the Entity Framework is used.”_

As extra information I got the package MiniProfiler.MVC4 3.0.10-beta1 what is working fine.  
Any idea?

---

<div class="post-metadata">

**Author:** ![Yaakov\_Ellis](https://sea2.discourse-cdn.com/flex016/user_avatar/community.miniprofiler.com/yaakov_ellis/32/961_2.png) [@Yaakov\_Ellis](https://community.miniprofiler.com/u/Yaakov_Ellis)\
**Post date:** [April 9, 2014, 8:25am UTC](https://community.miniprofiler.com/t/exception-using-ef6/253/2 "2014-04-09T08:25:57Z")

</div>

Can you please make sure that the `MiniProfiler.EF6.Initialize()` function is run _before_ any other db access is performed through EF?

---

<div class="post-metadata">

**Author:** ![carlbm](https://sea2.discourse-cdn.com/flex016/user_avatar/community.miniprofiler.com/carlbm/32/844_2.png) [@carlbm](https://community.miniprofiler.com/u/carlbm)\
**Post date:** [May 31, 2015, 12:06am UTC](https://community.miniprofiler.com/t/exception-using-ef6/253/3 "2015-05-31T00:06:04Z")

</div>

Hi  
I’m bashing my head with this one. I’ve put the line as the first thing in the App\_Start method and I’m still getting the exception. I even tried creating a constructor for the app and initialising it there but _still_ got the exception.

Do you know anything that could be starting up EF prior to Global.asax?

---

<div class="post-metadata">

**Author:** ![vikramjb](https://sea2.discourse-cdn.com/flex016/user_avatar/community.miniprofiler.com/vikramjb/32/873_2.png) [@vikramjb](https://community.miniprofiler.com/u/vikramjb)\
**Post date:** [March 1, 2016, 12:12am UTC](https://community.miniprofiler.com/t/exception-using-ef6/253/4 "2016-03-01T00:12:35Z")

</div>

On your Application\_Start, you can do this

` MiniProfilerEF6.Initialize();  
Database.SetInitializer\<Performance.DataModel.PerformanceContext\>(null);// Remove this if implementing database migration it gets rid of context has changed exception

`On your application begin request you can put this  
if (Request.IsLocal)  
{  
MiniProfiler.Start();  
}

Then on your Application\_EndRequest you can do this

MiniProfiler.Stop();
