james Makinde
james Makinde

Reputation: 973

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?

Upvotes: 14

Views: 27857

Answers (2)

Si Zi
Si Zi

Reputation: 1139

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;

Upvotes: 41

AnorZaken
AnorZaken

Reputation: 2114

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.

Upvotes: 4

Related Questions