title: EF - The context cannot be used while the model is being created exception during HTTP requests
body: <p>I am receiving "The context cannot be used while the model is being created." issue in my web application in one of my webpages.  This particular webpage POSTs to the server every 2-3 seconds to refresh the screen.  From my testing I found that If I have 2 or more browser instances open to this page, after several minutes I receive a "The context cannot be used while the model is being created" exception from deep in the repository.</p>
  
  <p>This code calls a "service" to retrieve the needed data.  This code is executed in an custom authorization attribute of the MVC Controller class.</p>
  
  <pre><code>// Code in custom "Authorization" attribute on the controller
  int? stationId = stationCookieValue;  // Read value from cookie
  RoomStationModel roomStationModel = RoomStationService.GetRoomStation(stationId); // Error occurs inside this call
  </code></pre>
  
  <p>Here is the "RoomStationModel"</p>
  
  <pre><code>public class RoomStationModel
  {
      [Key]
      public int RoomStationId { get; set; }
  
      public int? RoomId { get; set; }
      [ForeignKey("RoomId")]
      public virtual RoomModel Room { get; set; }
      /* Some other data properties.... */
   }
  
  public class RoomModel
  {
      [Key]
      public int RoomId { get; set; }
  
      public virtual ICollection&lt;RoomStationModel&gt; Stations { get; set; }
  }
  </code></pre>
  
  <p>Here is the code for the service call above:</p>
  
  <pre><code>public RoomStationModel GetRoomStation(int? roomStationId)
  {
      RoomStationModel roomStationModel = null;
      if (roomStationId.HasValue)
      {
          using (IRepository&lt;RoomStationModel&gt; roomStationRepo = new Repository&lt;RoomStationModel&gt;(Context))
          {
              roomStationModel = roomStationRepo.FirstOrDefault(rs =&gt; rs.RoomStationId == roomStationId.Value, false, new string[] { "Room" });
          }
      }
  
      return roomStationModel;
  }
  </code></pre>
  
  <p>Here's the repository....where the error occurs</p>
  
  <pre><code>    public class Repository&lt;TObject&gt; : IRepository&lt;TObject&gt; where TObject : class
      {
          protected MyContext Context = null;
  
          public Repository(IDataContext context)
          {
              Context = context as MyContext;
          }
  
          protected DbSet&lt;TObject&gt; DbSet { get { return Context.Set&lt;TObject&gt;(); } }
  
      public virtual TObject FirstOrDefault(Expression&lt;Func&lt;TObject, bool&gt;&gt; predicate, bool track = true, string[] children = null)
      {
          var objectSet = DbSet.AsQueryable();
  
          if (children != null)
              foreach (string child in children)
                  objectSet = objectSet.Include(child);
  
          if (track)
              return objectSet.Where(predicate).FirstOrDefault&lt;TObject&gt;(predicate);
  
          return objectSet.Where(predicate).AsNoTracking().FirstOrDefault&lt;TObject&gt;(predicate);
      }
  }
  </code></pre>
  
  <p>Screenshot of error:
  <img src="https://i.stack.imgur.com/tyUy8.png" alt="Screenshot of error occurring"></p>
  
  <p><strong>Stacktrace</strong>:</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.Include(String path)
     at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
     at System.Data.Entity.DbExtensions.Include[T](IQueryable`1 source, String path)
     at Vanguard.AssetManager.Data.Repository`1.FirstOrDefault(Expression`1 predicate, Boolean track, String[] children) in C:\Work\VanguardAssetManager\Main\Vanguard.AssetManager.Data\Repository.cs:line 100
     at Vanguard.AssetManager.Services.Business.RoomStationService.GetRoomStation(Nullable`1 roomStationId) in C:\Work\VanguardAssetManager\Main\Vanguard.AssetManager.Services\Business\RoomStationService.cs:line 61
     at Vanguard.AssetManager.Web.Attributes.RoomStationAuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) in C:\Work\VanguardAssetManager\Main\Vanguard.AssetManager.Web\Attributes\RoomStationAuthorizeAttribute.cs:line 52
     at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
     at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  </code></pre>
  
  <p>EF Version: 4.1 (Code first)</p>