sup*_*rio 5 c# postgresql timespan entity-framework-core
我在使用 EF Core 3.1 查询 PostgreSQL 数据库时遇到问题。
查询很简单
var gamesQuery = this.dbContext.Games.Where(game => game.StartTime > DateTime.Now).AsQueryable();
// 'request.TimeFrom' is of type System.TimeSpan and the value is populated
gamesQuery = gamesQuery.Where(game => game.StartTime.TimeOfDay >= request.TimeFrom);
// .ToList()-int here causes the exception.
var games = gamesQuery.ToList();
Run Code Online (Sandbox Code Playgroud)
异常消息明确指出查询无法翻译:
“无法翻译 LINQ 表达式 'DbSet\r\n .Where(g => g.StartTime > DateTime.Now)\r\n .Where(g => g.StartTime.TimeOfDay >= __request_TimeFrom_0)'。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用来显式切换到客户端评估。请参阅https://go.microsoft.com /fwlink/?linkid=2101038了解更多信息。”
问题是相同的查询在 .NET Core 2.2 中运行良好。我还没有发现任何有关该问题的信息。
有人知道这个的原因是什么,还是我错过了什么?
目前 PostgreSQL EF Core 3.x 查询提供程序不支持翻译DateTime.TimeOfDay- 请参阅源代码TODO中的注释。
最有可能的是它在 2.x 中通过默默地使用客户端评估来“工作”。但隐式客户端评估在 3.0 中已被删除,并且无法重新打开。
您可以尝试以下等效构造:
.Where(game => (game.StartTime - game.StartTime.Date) >= request.TimeFrom)
Run Code Online (Sandbox Code Playgroud)
至少它不会产生上述异常。
如果它不起作用,请听取他们的建议,并通过AsEnumerable()在不可翻译表达式之前的适当位置插入来显式切换到客户端评估。
| 归档时间: |
|
| 查看次数: |
3143 次 |
| 最近记录: |