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

2019-08-22 11:59发布

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!

2条回答
成全新的幸福
2楼-- · 2019-08-22 12:38

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.

查看更多
地球回转人心会变
3楼-- · 2019-08-22 12:55

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.

查看更多
登录 后发表回答