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?
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
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.
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 .
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.
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
;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With