简体   繁体   English

ASP.NET MVC Core和Entity Framework中的ToListAsync无法正常工作

[英]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 . 我在ASP.NET MVC Core和Entity Framework中有以下代码,当我执行ToListAsync时出现以下错误。

Additional information: The source IQueryable doesn't implement IDbAsyncEnumerable. 附加信息:源IQueryable未实现IDbAsyncEnumerable。 Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations. 只有实现IDbAsyncEnumerable的源才能用于Entity Framework异步操作。 For more details see http://go.microsoft.com/fwlink/?LinkId=287068 . 有关详细信息,请参阅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. 我的上下文类扩展自DbContext ,我使用了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()

ToListAsync() is defined in both System.Data.Entity and Microsoft.EntityFrameworkCore. ToListAsync()在System.Data.Entity和Microsoft.EntityFrameworkCore中定义。 If you import both, it will default to the definition in System.Data.Entity generating the above error. 如果同时导入两者,则默认为System.Data.Entity中的定义,生成上述错误。 Remove System.Data.Entity and add try adding using Microsoft.EntityFrameworkCore; 删除System.Data.Entity并使用Microsoft.EntityFrameworkCore添加try添加;

EF6 != EF Core. 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. ToListAsync是一个EF Core功能,您尝试在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. 如果要使用此功能,则必须使用EF Core DbContext - 但是,您可能再次使用代码中其他位置的EF6功能,而这些功能将停止工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM