gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:29:25 25 4
gpt4 key购买 nike

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

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

Conversion failed when converting date and/or time from character string.

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

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

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

和/或

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


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

public class DeskReading
{
public int DeskReadingId { get; set; }
//... some other fields ...
public DateTime Timestamp { get; set; }
}

我查询值的 LINQ 如下所示:

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

readings.ToList();

由此生成的 SQL 如下所示:

SELECT [gr].[DeskReadingId] --...some other fields ...
FROM [DeskReadings] AS [gr]
WHERE [gr].[Timestamp] > '2017-05-01T00:00:00.0000000'
ORDER BY [gr].[Timestamp]

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

如果我通过 SSMS 直接在 SqlServer 上运行该 SQL,我会得到同样的错误:

enter image description here

但如果我将过滤器更改为使用 '2017-05-01 00:00:00',它会正常工作:

SELECT [gr].[DeskReadingId] --...some other fields ...
FROM [DeskReadings] AS [gr]
WHERE [gr].[Timestamp] > '2017-05-01 00:00:00'
ORDER BY [gr].[Timestamp]

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

CREATE TABLE [dbo].[DeskReadings](
[DeskReadingId] [int] IDENTITY(1,1) NOT NULL,
[SoilMoistureSensor1] [int] NOT NULL,
[SoilMoistureSensor2] [int] NOT NULL,
[LightLevel] [int] NOT NULL,
[TemperatureF] [real] NOT NULL,
[HumidityPercent] [real] NOT NULL,
[Timestamp] [datetime] NOT NULL,
CONSTRAINT [PK_dbo.DeskReadings] PRIMARY KEY CLUSTERED
(
[DeskReadingId] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

最佳答案

在查看 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}";

格式 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; }
}

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

关于c# - 如何让 EntityFrameworkCore 生成的 SQL 为 DateTime 对象使用正确的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52048935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com