【发布时间】:2021-03-01 09:44:53
【问题描述】:
我是刚开始在项目中使用 EF Core 5 的新手,遇到以下查询问题:
TimeSpan bookTimeToLive = TimeSpan.FromHours(10);
IList<Book>? expiredBooks = dbContext.Value.Books.AsQueryable()
.Where(x => DateTime.UtcNow - x.UtcTimeStamp > bookTimeToLive)
.ToList();
// Get list of expired Books to remove them
dbContext.Value.RemoveRange(expiredBooks);
await dbContext.Value.SaveChangesAsync(cancellationToken);
我的目标是删除所有过期的图书(它们的时间戳超过了我想要跟踪它们的时间)。
这样,我得到了例外:
The LINQ expression 'DbSet<Books>()
.Where(d => DateTime.UtcNow - d.UtcTimeStamp > __bookTimeToLive_0)' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See
https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
经过一番挖掘,我意识到这是因为 EF 无法将我的 DateTime 比较解析为 SQL 查询,所以我尝试使用 https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.sqlserverdbfunctionsextensions.datediffhour?view=efcore-5.0#Microsoft_EntityFrameworkCore_SqlServerDbFunctionsExtensions_DateDiffHour_Microsoft_EntityFrameworkCore_DbFunctions_System_DateTime_System_DateTime_ 中的 DbFunctions.DateDiffHour() 方法
现在的问题是,即使我在课堂上安装并导入了 Nuget EF Core 5,我也无法访问任何 DbFunctions 方法:
这可能是一个错误还是我没有正确使用这些类?这是我想要完成的正确方法吗? 干杯!
【问题讨论】:
-
它们是扩展方法,但您缺少导入,请尝试添加
using Microsoft.EntityFrameworkCore; -
@DavidG 导入已添加到我的班级顶部
-
确保你已经安装了包
Microsoft.EntityFrameworkCore.SqlServer
标签: c# entity-framework entity-framework-core entity-framework-5