Entities Framework 6 alpha 2 - Async Patterns

asp.net-mvc asp.net-mvc-4 azure-sql-database c# entity-framework-6

Question

I recently have a project with MVC 4, .NET 4.5 and using EF 5.0 (Database First) using the EF 5.0 DbContext Generator. I then upgraded through Nuget Manager to EF 6.0 alpha 2. I wanted to use the new async patterns but for some reason I dont have .ToListAsync() available. I am referencing the context in a similiar fashion:

public class HomeController : Controller
{
    TestContext db = new TestContext();

    public async Task<ActionResult> Index()
    {
        var keywords = await db.Keywords.ToListAsync();

        return View(keywords);
    }
}

Is this not available in Database First, and only available in Code First? How can I make something like the example above work? I am using SQL Azure.

Accepted Answer

Async. extension methods are defined in EF related System.Data.Entity.IQueryableExtensions static class. So you must add:

using System.Data.Entity;

at the beginning of your controller class file to be able to call them.


Popular Answer

I think that you can use Task.Run like this

public async Task<ActionResult> Index()
{
    var keywords = await Task.Run(() => db.Keywords.ToList());

    return View(keywords);
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why