title: System.ObjectDisposedException: Cannot access a disposed object
body: <p>I have the following GenericRepository:-</p>
  
  <pre><code>    public class GenericRepository&lt;T&gt; : IGenericRepository&lt;T&gt; where T : class
      {
      public readonly SportsStore2Context Context;
      protected DbSet&lt;T&gt; DbSet;
  
      public GenericRepository(SportsStore2Context context)
      {
          Context = context;
          DbSet = context.Set&lt;T&gt;();
      }
  
      public async Task&lt;T&gt; Get&lt;TKey&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; filter = null, string includeProperties = "")
  
      {
          IQueryable&lt;T&gt; query = Context.Set&lt;T&gt;();
          query = IncludePropertiesQuery(query, includeProperties);
  
          if (filter != null)
          {
              query = query.Where(filter);
          }
  
          return await query.SingleOrDefaultAsync();
      }
  
      public async Task&lt;List&lt;T&gt;&gt; GetAll(Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderBy = null, string includeProperties = "")
      {
          IQueryable&lt;T&gt; query = Context.Set&lt;T&gt;();
          query = IncludePropertiesQuery(query, includeProperties);
  
          if (orderBy != null)
          {
              query = orderBy(query);
          }
  
          var collection = await query.ToListAsync();
          return collection;
      }
  
      public async Task Add(T entity, Expression&lt;Func&lt;T, bool&gt;&gt; filter = null)
      {
          var existing = await Get&lt;T&gt;(filter);
  
          if (existing == null)
          {
              Context.Set&lt;T&gt;().Add(entity);
              Save();
          }
  
      }
  
  
      public void Update(T entity)
      {
  
          Context.Set&lt;T&gt;().Update(entity);
          Save();
      }
  
      public void Delete(T entity)
      {
          var dbSet = Context.Set&lt;T&gt;();
          if (Context.Entry(entity).State == EntityState.Detached)
          {
              dbSet.Attach(entity);
          }
          dbSet.Remove(entity);
  
          Save();
      }
  
      private void Save()
      {
          try
          {
              Context.SaveChanges();
          }
          catch (Exception e)
          {
              Console.WriteLine(e);
              throw;
          }
      }
  
      private IQueryable&lt;T&gt; IncludePropertiesQuery(IQueryable&lt;T&gt; query, string includeProperties = "")
      {
          includeProperties = includeProperties.Trim() ?? string.Empty;
          foreach (var includeProperty in includeProperties.Split
              (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
          {
              query = query.Include(includeProperty);
          }
  
          return query;
  
      }
  
  }
  </code></pre>
  
  <p>The Get and GetAll work fine, however when I try to Add something to the database, I am getting a "System.ObjectDisposedException: Cannot access a disposed object" error.</p>
  
  <p>I have declared the repository as follows in the Configure of the Startup:-</p>
  
  <pre><code>   services.AddScoped(typeof(IGenericRepository&lt;&gt;), typeof(GenericRepository&lt;&gt;));
  </code></pre>
  
  <p>What could be the problem?  Am I declaring the context erroneously?</p>
  
  <p>Thanks for your help and time.</p>
  
  <p>UPDATE</p>
  
  <p>Removing the await(async) does work correctly</p>
  
  <pre><code>    public void Add(T entity, Expression&lt;Func&lt;T, bool&gt;&gt; filter = null)
      {
          var existing = Get&lt;T&gt;(filter);
          if (existing.Result != null) return;
          Context.Add(entity);
          Save();
      }
  </code></pre>
  
  <p>Is this correct?</p>