将我的项目从(dotnet core 2/ef core 2)升级到(dotnet core 3/ef core 3)后,几乎所有的实体框架 LINQ 查询都无法正常工作。尽管我已经阅读了这篇文章,但仍不清楚该怎么做。
以下是一些我遇到问题的示例:
var league = await dbContext.League.LastAsync();
虽然这段代码在 ef core 2 中可以正常工作,但在 ef core 3 中会抛出异常。 我找到的唯一解决方法是以下代码,但仍不符合我的要求,因为它不像以前那样是异步的。
var league = dbContext.League.AsEnumerable().Last();
另一个抛出相同异常的示例是以下代码:
var user = await dbContext.User.FirstOrDefaultAsync(u =>
u.UserId == userId && string.Equals(u.Token, token, StringComparison.InvariantCulture));
我仍然可以使用AsEnumerable(),但异步版本的FirstOrDefault在那里不可用,所以这不是一个选择。有人能为我提供指导吗?
编辑
这是异常:
System.InvalidOperationException: The LINQ expression 'Last<League>(DbSet<League>)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Last
而没有指定顺序有点毫无意义... - grek40FirstOrDefaultAsync()
是可用的。你只是缺少一个using <nmspc>;
。 - H H