DbSet<> 不包含"ToListAsync()"的定义,尽管我将 .NET 3.1 与 EF Core 一起使用



因此,我尝试将异步方法添加到路由、接口、存储库等中,并对所有内容进行了正确的编码。一切看起来都很棒,但当我尝试运行应用程序时,我会收到错误:

Error CS1061: 'DbSet<Hotel>' does not contain a definition for 'ToListAsync' and no accessible extension method 'ToListAsync' accepting a first argument of type 'DbSet<Hotel>' could be found (are you missing a using directive or an assembly reference?) (CS1061)

我下载了EFCore包,甚至对大部分内容进行了硬编码,因为.NET并没有检测到任何与异步方法相关的内容。

你知道我为什么会犯这个错误吗?我找不到任何解决办法。编辑:我在Mac电脑上

ToListAsync函数是EntityFrameworkCore包中定义的扩展方法。您需要添加using语句,如下所示:

using Microsoft.EntityFrameworkCore;

您可以参考这个例子。为像我这样的NOOBS加油。


using ApiApp.Data;
using ApiApp.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ApiApp.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
private readonly DataContext _context;
public UserController(DataContext context)
{
_context = context;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<AppUser>>> GetUsers()
{
var users = await _context.Users.ToListAsync();
return users;
}
[HttpGet("{id}")]
public async Task<ActionResult<AppUser>> GetUser(int id)
{
return await _context.Users.FindAsync(id);
}
}
}

最新更新