Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToListAsync in ASP.NET MVC Core and Entity Framework not working

I have the following code in ASP.NET MVC Core and Entity Framework and I get the following error when I do a ToListAsync.

Additional information: The source IQueryable doesn't implement IDbAsyncEnumerable. Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.

This is my code:

var states = mDbContext.State.ToListAsync();
var countries = mDbContext.Country.ToListAsync();

mMemoryCache.Set(Countries, await countries,
    new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.MaxValue));
mMemoryCache.Set(States, await states,
   new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.MaxValue));

My context class extends from DbContext, and I used EF 6.

public virtual DbSet<Country> Country { get; set; }

public virtual DbSet<State> State { get; set; }

Any idea why I can't perform ToListAsync() even when I have everything installed?

like image 450
james Makinde Avatar asked Dec 23 '16 17:12

james Makinde


People also ask

Does ASP tolistasync work on ASP NET Core?

ToListAsync () fails on ASP NET CORE but ToList () works - Error "Sequence contains no elements." this function works fine with ASP.NET MVC but with the migration to .NET CORE I have the error

Is tolistasync compatible with EF6 dbcontext?

EF6 != EF Core. They are feature similar to some extent but they are not compatible. ToListAsync is an EF Core feature and you're trying to use it on an EF6 DbContext. That's why it doesn't work.

Where is the tolistasync method in the Entity Framework?

The ToListAsync method is part of the QueryableExtensions class which is in the System.Data.Entity namespace and part of the EntityFramework.dll library. This means that you need import the namespace (i.e. using System.Data.Entity; ) as well as reference EntityFramework.dll .

How to add MVC controller with views using Entity Framework?

The Add MVC Controller with views, using Entity Framework dialog box appears: In Model class, select Student. In Data context class, select SchoolContext. Accept the default StudentsController as the name. Click Add.


2 Answers

ToListAsync() is defined in both System.Data.Entity and Microsoft.EntityFrameworkCore. If you import both, it will default to the definition in System.Data.Entity generating the above error. Remove System.Data.Entity and add try adding using Microsoft.EntityFrameworkCore;

like image 147
Si Zi Avatar answered Sep 19 '22 06:09

Si Zi


EF6 != EF Core. They are feature similar to some extent but they are not compatible.

ToListAsync is an EF Core feature and you're trying to use it on an EF6 DbContext.

That's why it doesn't work. If you want to use this feature you have to use an EF Core DbContext - but then again you might be using EF6 only features elsewhere in your code, and those would stop working instead.

like image 27
AnorZaken Avatar answered Sep 19 '22 06:09

AnorZaken