I'm wanting to use the async/await pattern in my ASP.NET MVC controller. I'm getting the error
The ObjectContext instance has been disposed and can no longer be used
Ultimately, I'm wanting to cache my result but for now I think the first step is to just figure out how to work with EF and return with async/await
public class SpeakerController : Controller
{
public async Task<ActionResult> Index()
{
using (var context = new MultiTenantContext())
{
var speakersAll = await context.Speakers.ToListAsync();
return View("Index", speakersAll);
}
}
Solution Found:
private MultiTenantContext context = new MultiTenantContext();
public async Task<ActionResult> Index()
{
var speakersAll = await context.Speakers.ToListAsync();
return View("Index", speakersAll);
}
As I said in the comment below, pulling context out of the using causes it not to be disposed when the ToListAsync() actually executes.