Can not use .ToListAsync() extension method (.net 4.7)

杀马特。学长 韩版系。学妹 提交于 2019-12-11 15:27:21

问题


Im am trying to use extension method .ToListAsync() but for some reason this extension method not available for me.

My set up as follows:

  • Web Project (.Net 4.7) here i did include using System.Data.Entity;
  • DataAcess Project (.Net 4.7) here I interlude Entity Frame Work v6.2

My Web Project does reference my DataAccess project.

Im not sure where i went wrong. Can somebody please advise?

Thank you!


回答1:


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.

Note that in classic .Net Framework projects, references are not transitive. In other words, if you want to use classes from a library, you must reference it in every project. This has changed in .Net Core though.




回答2:


While the .ToListAsync() method is made available by referencing the EntityFramework.dll and using System.Data.Entity;, it is only available on types that implement the IQueryable interface.

An example of it being used:

private async Task<List<Book>> GetAllBooksAsync() {

    var books = new List<Book>();
    var query = from item in books select item;
    return await query.AsQueryable().ToListAsync();
}

As a note, if you can't see the ToListAsync() method, you may be missing the using System.Data.Entity; in your class.



来源:https://stackoverflow.com/questions/48372847/can-not-use-tolistasync-extension-method-net-4-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!