title: Entity Framework The context cannot be used while the model is being created
body: <p>My unit of work class is mentioned below and I am using Ninject and I have tried injecting <code>IUnitOfWork</code> per request per thread scope, transient etc. but I am still getting error which is:</p>
  
  <blockquote>
    <p>"Message":"An error has occurred.","ExceptionMessage":"The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.","ExceptionType":"System.InvalidOperationException</p>
  </blockquote>
  
  <p>I get this error when i make two web API (get) calls at the same time using angularJS and it shows error at the point <code>_context.Set&lt;TEntity&gt;().FirstOrDefault(match);</code>  </p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork, IDisposable
  {
      private My_PromotoolEntities _uowDbContext = new My_PromotoolEntities();
  
      private Dictionary&lt;string, object&gt; _repositories;
  
  
      // Do it like this if no specific class file
      private GenericRepository&lt;MysPerson&gt; _personRepository;
      //private GenericRepository&lt;MysDataSource&gt; dataSourcesRepository;
      //private GenericRepository&lt;MysCountry&gt; countryMasterRepository;
  
  
      // Or like this if with specific class file.
      private DataSourceRepository _dataSourcesRepository;
      private CustomerRepository _customerRepository;
      private DeviceRepository _deviceRepository;
      private DeviceRegistrationRepository _deviceRegistrationRepository;
      private EmailQueueRepository _emailQueueRepository;
  
  
      public void SetContext(My_PromotoolEntities context)
      {
          _uowDbContext = context;
      }
  
  
      public void CacheThis(object cacheThis, string keyName, TimeSpan howLong)
      {
          Cacheing.StaticData.CacheStaticData(cacheThis, keyName, howLong);
      }
      public object GetFromCache(string keyName)
      {
          return Cacheing.StaticData.GetFromCache(keyName);
      }
  
  
      public GenericRepository&lt;T&gt; GenericRepository&lt;T&gt;() where T : BaseEntity
      {
          if (_repositories == null)
          {
              _repositories = new Dictionary&lt;string, object&gt;();
          }
  
          var type = typeof(T).Name;
  
          if (!_repositories.ContainsKey(type))
          {
              var repositoryType = typeof(GenericRepository&lt;&gt;);
              var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), _uowDbContext);
              _repositories.Add(type, repositoryInstance);
          }
          return (GenericRepository&lt;T&gt;)_repositories[type];
      }
  
      public GenericRepository&lt;MysPerson&gt; PersonRepository
      {
          get
          {
              if (this._personRepository == null)
              {
                  this._personRepository = new GenericRepository&lt;MysPerson&gt;(_uowDbContext);
              }
              return _personRepository;
          }
      }
      public DataSourceRepository DataSourcesRepository
      {
          get
          {
              if (this._dataSourcesRepository == null)
              {
                  this._dataSourcesRepository = new DataSourceRepository(_uowDbContext);
              }
              return _dataSourcesRepository;
          }
      }
      public CustomerRepository CustomerRepository
      {
          get
          {
              if (this._customerRepository == null)
              {
                  this._customerRepository = new CustomerRepository(_uowDbContext);
              }
              return _customerRepository;
          }
      }
      public DeviceRepository DeviceRepository
      {
          get
          {
              if (this._deviceRepository == null)
              {
                  this._deviceRepository = new DeviceRepository(_uowDbContext);
              }
              return _deviceRepository;
          }
      }
      public DeviceRegistrationRepository DeviceRegistrationRepository
      {
          get
          {
              if (this._deviceRegistrationRepository == null)
              {
                  this._deviceRegistrationRepository = new DeviceRegistrationRepository(_uowDbContext);
              }
              return _deviceRegistrationRepository;
          }
      }
  
      public EmailQueueRepository emailQueueRepository
      {
          get
          {
              if (this._emailQueueRepository == null)
              {
                  this._emailQueueRepository = new EmailQueueRepository(_uowDbContext);
              }
              return _emailQueueRepository;
          }
      }
  
  
  
  
      /// &lt;summary&gt;
      /// Commits all changes to the db. Throws exception if fails. Call should be in a try..catch.
      /// &lt;/summary&gt;
      public void Save()
      {
          try
          {
              _uowDbContext.SaveChanges();
          }
          catch (DbEntityValidationException dbevex)
          {
              // Entity Framework specific errors:
  
              StringBuilder sb = new StringBuilder();
              var eve = GetValidationErrors();
              if (eve.Count() &gt; 0)
              {
                  eve.ForEach(error =&gt; sb.AppendLine(error));
              }
  
              ClearContext();
  
              // Throw a new exception with original as inner.
              var ex = new Exception(sb.ToString(), dbevex);
              ex.Source = "DbEntityValidationException";
              throw ex;
          }
          catch (Exception)
          {
              ClearContext();
              throw;
          }
      }
  
      private void ClearContext()
      {
          DetachAll();
      }
  
      private void DetachAll()
      {
          foreach (DbEntityEntry dbEntityEntry in _uowDbContext.ChangeTracker.Entries())
          {
  
              if (dbEntityEntry.Entity != null)
              {
                  dbEntityEntry.State = EntityState.Detached;
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Checks for EF DbEntityValidationException(s).
      /// &lt;/summary&gt;
      /// &lt;returns&gt;Returns a List of string containing the EF DbEntityValidationException(s).&lt;/returns&gt;
      public List&lt;string&gt; GetValidationErrors()
      {
          if (_uowDbContext.GetValidationErrors().Count() != 0)
          {
              return _uowDbContext.GetValidationErrors().Select(e =&gt; string.Join(Environment.NewLine, e.ValidationErrors.Select(v =&gt; string.Format("{0} - {1}", v.PropertyName, v.ErrorMessage)))).ToList();
          }
          return null;
      }
  
  
  
      private bool disposed = false;
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  _uowDbContext.Dispose();
              }
          }
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>