简体   繁体   English

EF Core 6 和 SQLite 中的 TimeOfDay 问题

[英]TimeOfDay issue in EF Core 6 and SQLite

I have a problem querying a SQLite database using EF Core 6.我在使用 EF Core 6 查询 SQLite 数据库时遇到问题。

The query is pretty simple:查询非常简单:

_context.SomeTable
        .AsNoTracking()
        .Where(x => x.SomeDateTimeColumn.Value.TimeOfDay <= DateTime.Now.TimeOfDay)
        .ToList();

The exception message states that the query could not be translated:异常消息指出无法翻译查询:

The LINQ expression 'DbSet().Where(t => t.SomeDateTimeColumn.Value.TimeOfDay <= DateTime.Now.TimeOfDay)' could not be translated.无法翻译 LINQ 表达式 'DbSet().Where(t => t.SomeDateTimeColumn.Value.TimeOfDay <= DateTime.Now.TimeOfDay)'。 Either rewrite the query in a form that can be translated, or switch to client evaluation...要么以可以翻译的形式重写查询,要么切换到客户评估......

My problem is that, according to the documentation, DateTime.TimeOfDay should be supported by the SQLite database provider.我的问题是,根据文档,SQLite 数据库提供程序应该支持DateTime.TimeOfDay See here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/functions#date-and-time-functions见这里: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/functions#date-and-time-functions

Does anyone know the reason for this or could point me to what I'm missing?有谁知道这是什么原因,或者可以指出我错过了什么?

TimeSpan values are not yet supported by EF Core. EF Core 尚不支持TimeSpan值。 Please upvote issue #18844 .投票 #18844问题。

Hmm, you might be able to do it using Ticks .嗯,您也许可以使用Ticks来做到这一点。 But the precision might be kinda bad.但是精度可能有点差。

x.SomeDateTimeColumn.Value.Ticks % 864000000000 <= DateTime.Now.Ticks % 864000000000

For better precision, you can manually map the julianday function.为了获得更好的精度,您可以手动 mapjulianday function。

I think it might be related to the way DateTime works.我认为这可能与 DateTime 的工作方式有关。

Instead of using:而不是使用:

_context.SomeTable
    .AsNoTracking()
    .Where(x => x.SomeDateTimeColumn.Value.TimeOfDay <= DateTime.Now.TimeOfDay)
    .ToList();

Try this:尝试这个:

_context.SomeTable
    .AsNoTracking()
    .Where(x => x.SomeDateTimeColumn <= DateTime.Now)
    .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM