如何让 EntityFrameworkCore 生成的 SQL 为 DateTime 对象使用正确的格式?

Joh*_*ght 6 c# sql-server entity-framework-core

我正在使用Microsoft.EntityFrameworkCore.SqlServer v2.1.2(并且也尝试过 v2.2.0-preview1-35029)并使用 LINQ 从 Azure SqlServer 数据库中获取实体集合,在 DateTime 字段上进行过滤。

但是,由 LINQ 语句生成的 SQL 使用基于字符串的 DateTime 值,SqlServer 拒绝此错误:

从字符串转换日期和/或时间时转换失败。

我可以修改 SQL 语句以更改日期时间格式,以便查询正常工作(有关详细信息,请参见下文),但我不知道如何让框架生成相同的日期时间格式。

虽然 EntityFrameworkCore 仍然有点新,但似乎这是一个非常简单的用例,所以我假设我做错了什么,这不是框架问题。

如何防止 EF 在 SQL 中生成无效的日期时间值?

和/或

如何让生成的 SQL 为 DateTime 对象使用另一种格式?


我正在使用的 EntityFramework 模型如下所示:

public class DeskReading
{
    public int DeskReadingId { get; set; }
    //... some other fields ...
    public DateTime Timestamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的 LINQ 查询值如下所示:

IQueryable<DeskReading> readings = 
    _dbContext.DeskReadings
            .OrderBy(gr => gr.Timestamp)
            .Where(gr => gr.Timestamp > new DateTime(2017, 05, 01));

readings.ToList();
Run Code Online (Sandbox Code Playgroud)

由它生成的 SQL 如下所示:

public class DeskReading
{
    public int DeskReadingId { get; set; }
    //... some other fields ...
    public DateTime Timestamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

请注意,过滤器的值为 '2017-05-01T00:00:00.0000000'

如果我通过 SSMS 直接在 SqlServer 上运行该 SQL,则会出现相同的错误:

在此处输入图片说明

但是如果我将过滤器更改为 use '2017-05-01 00:00:00',它就可以正常工作:

IQueryable<DeskReading> readings = 
    _dbContext.DeskReadings
            .OrderBy(gr => gr.Timestamp)
            .Where(gr => gr.Timestamp > new DateTime(2017, 05, 01));

readings.ToList();
Run Code Online (Sandbox Code Playgroud)

根据要求,这是表的创建脚本:

SELECT [gr].[DeskReadingId] --...some other fields ...
FROM [DeskReadings] AS [gr]
WHERE [gr].[Timestamp] > '2017-05-01T00:00:00.0000000'
ORDER BY [gr].[Timestamp]
Run Code Online (Sandbox Code Playgroud)

Dav*_*ine 4

在查看GitHub上的源代码时, EntityFrameworkCore根据它认为表达式中的列所使用的 StoreType 使用条件格式。例如,您看到的格式显然适用于datetime2. datetime将列与格式化字符串进行比较时,可能会出现您遇到的错误datetime2

这是我指的来源,有三个字符串常量表示 C# 值的格式DateTime

    private const string DateFormatConst = "{0:yyyy-MM-dd}";
    private const string DateTimeFormatConst = "{0:yyyy-MM-ddTHH:mm:ss.fffK}";
    private const string DateTime2FormatConst = "{0:yyyy-MM-ddTHH:mm:ss.fffffffK}";
Run Code Online (Sandbox Code Playgroud)

格式 https://github.com/aspnet/EntityFrameworkCore/blob/release/2.2/src/EFCore.SqlServer/Storage/Internal/SqlServerDateTimeTypeMapping.cs#L18-L20

条件逻辑 https://github.com/aspnet/EntityFrameworkCore/blob/release/2.2/src/EFCore.SqlServer/Storage/Internal/SqlServerDateTimeTypeMapping.cs#L70-L74

要解决此特定问题,您可以将模型归因如下:

public class DeskReading
{
    public int DeskReadingId { get; set; }

    [Column(TypeName="datetime")]
    public DateTime Timestamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这将强制比较将其视为StoreTypeofdatetime并正确格式化它。