我将 ef core 与 Oracle.EntityFrameworkCore 结合使用。我在查询运行时遇到错误
无法翻译 LINQ 表达式“xy”。
附加信息:
方法“System.Data.Entity.DbFunctions.AddMilliseconds”的翻译失败”。
型号:
public class Session
{
public virtual Datetime STARTED_AT { get; set; }
public virtual int TIMEOUT { get; set; }
}
查询:
using System.Data.Entity;
var timeoutList = sessionRepository.Table.Include(m => m.Definition)
.Where(x => DbFunctions.DiffMilliseconds(x.STARTED_AT, now).Value > x.TIMEOUT)
如何重新调整查询阶段?
您可以计算内存中日期之间的毫秒差异。
using System.Linq;
var timeoutList = sessionRepository.Table
.Include(m => m.Definition)
.AsEnumerable()
.Where(x => (now - x.STARTED_AT).TotalMilliseconds > x.TIMEOUT)
.ToList();
如果您使用的是最新版本的 ef core,则不应导入 System.Data.Entity 命名空间,以前可以在其中找到 DbFunctions 类。
无论如何,根本不需要 DbFunctions,您可以像下面这样重新表述您的查询。
var timeoutList = sessionRepository.Table.Include(m => m.Definition)
.Where(x => X.STARTED_AT.AddMilliseconds(x.TIMEOUT) < now)