Last post Jan 23, 2020 08:01 AM by EngSH
Jan 22, 2020 11:26 AM|EngSH|LINK
I am getting error on both ToListAsync & ToList
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
return await _context.Product.ToListAsync();
I do not know which one to use in this case.
'DbSet<Product>' does not contain a definition for 'ToListAsync' and no accessible extension method 'ToListAsync' accepting a first argument of type 'DbSet<Product>' could be found (are you missing a using directive or an assembly reference?)
Jan 22, 2020 03:26 PM|mgebhard|LINK
You forgot to add...
Visual Studio provides an animated hint if you hover over the compiler error that gives a list of possible solution you can select.
Jan 22, 2020 03:42 PM|PatriceSc|LINK
Try to add:
to bring those extension methods in scope. See https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.queryableextensions.tolistasync?view=entity-framework-6.2.0 where
you find it is an extension method and https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods to
better understand the feature:
"Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier.
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive."
Jan 23, 2020 02:18 AM|Sherry Chen|LINK
Hi EngSH ,
For EF Core , you could press Atl+Enter or Ctrl+. to add the following assembly reference
Jan 23, 2020 08:01 AM|EngSH|LINK
@mgebhard: I resolved it by adding below