cannot access a disposed object Datacontext asp.net 5
<p>In asp.net 5/core I want to delete an item from my db with a delay using async.
I'm using DI to inject my Datacontext (db) in the class.</p>

<p>I created the following method:</p>

<pre><code>public async void DeleteItemAfterTimeAsync(int itemId, TimeSpan timeSpan)
{
    // give user time to fill out data
    await Task.Delay(timeSpan);
    var item = db.Items.FirstOrDefault(p =&gt; p.Id == itemId);
    if (item!= null &amp;&amp; item.Status == someStatus)
    {
        db.Items.Remove(item);
        db.SaveChanges();
    }
}
</code></pre>

<p>After the given time the method is executed as planned but I get an exception: 'cannot access a disposed object' which is probably because the service is disposed because of the delay.
Should i resolve a new instance of my datacontext in the method or how should I fix this?
Any ideas?
Thanks for helping out in advance.</p>

<p>UPDATE:</p>

<p>Calling the method:</p>

<pre><code>await DeleteItemAfterTimeAsync(item.Id, new TimeSpan(0, 1, 0));
</code></pre>

<p>Updated method:</p>

<pre><code>public async Task DeleteItemAfterTimeAsync(int itemId, TimeSpan timeSpan)
{
   // give user time to fill out data
   await Task.Delay(timeSpan);
   var item = db.Items.FirstOrDefault(p =&gt; p.Id == itemId);
   if (item != null &amp;&amp; item.Status == someStatus)
   {
      db.Items.Remove(item);
      db.SaveChanges();
   }    
}
</code></pre>

<p>Exception occurs on db.Items.Remove(item);</p>