title: How can I prevent EF "The context cannot be used while the model is being created" errors?
body: <p>Looking at my Elmah error logs, I am seeing a few <code>InvalidOperationException</code>s from Entity Framework that deal with:</p>
  
  <pre><code>The context cannot be used while the model is being created.
  </code></pre>
  
  <p>This is with the latest EF CodeFirst library from Nuget.  The only information I have been able to find on the net is that it is being caused by having data contexts as singletons, which is most certainly not my case.  In my Windsor installer, my EF unit of work structure is being registered with:</p>
  
  <pre><code>container.Register(Component.For&lt;IUnitOfWork&gt;()
                              .ImplementedBy&lt;EFUnitOfWork&gt;()
                              .LifeStyle
                              .PerWebRequest);
  </code></pre>
  
  <p>I am able to recreate the error by hitting F5 in VS to start a debugging sessions, and while IIS is spinning up load up a second webpage to the debug session.</p>
  
  <p>I suspect it is because the user is trying to access the system while Asp.net has unloaded due to the lack of activity, which makes sense as my product is currently in a very very small beta test.  However, since real people are using the website with live data, I need as little errors occurring as possible. </p>
  
  <p>Does anyone have any idea how to prevent this from occurring?</p>
  
  <p><hr>
  <b>Edit:</b> I updated my windsor controller to now contain the following code:</p>
  
  <pre><code>        container.Register(Component.For&lt;IUnitOfWork&gt;().ImplementedBy&lt;EFUnitOfWork&gt;().LifeStyle.PerWebRequest);
          using (var context = new MyJobLeadsDbContext())
          {
              context.Set&lt;UnitTestEntity&gt;().Any();
          }
  </code></pre>
  
  <p>However, when I attempt to perform a 2nd web request while IIS is loading the application, the previous error still occurs</p>
  
  <p><hr>
  <b>Edit 2:</b> As requested, here is the stack</p>
  
  <pre><code>   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
     at System.Data.Entity.Internal.InternalContext.Initialize()
     at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
     at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
     at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
     at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
     at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
     at MyApp.DomainModel.Queries.Users.UserByEmailQuery.Execute() in C:\Users\KallDrexx\Documents\Projects\MyApp\MyApp.DomainModel\Queries\Users\UserByEmailQuery.cs:line 44
     at MyApp.Infrastructure.MyAppMembershipProvider.GetUser(String email, Boolean userIsOnline) in C:\Users\KallDrexx\Documents\Projects\MyApp\MyApp\Infrastructure\MyAppMembershipProvider.cs:line 102
     at System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline)
     at System.Web.Security.Membership.GetUser()
     at MyApp.MyAppBaseController.Initialize(RequestContext requestContext) in C:\Users\KallDrexx\Documents\Projects\MyApp\MyApp\MyAppBaseController.cs:line 23
     at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
     at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
     at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass6.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
     at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d()
     at System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f)
     at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
     at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
     at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
     at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>