在 DateTime 比较期间,如何强制实体框架使用 datetime 而不是 datetime2?

2023-11-30

我正在使用实体框架来检索记录,其中一个过滤器是datetime.

它生成如下查询:

([Extent1].[TxnDateTime] >= convert(datetime2, '2015-02-21 00:00:00.0000000', 121)) 
AND ([Extent1].[TxnDateTime] <= convert(datetime2, '2017-02-21 23:59:59.9999999', 121))

有没有办法让 EF 转换为datetime代替datetime2?看起来快多了。

我希望 EF 生成这个:

[Extent1].[TxnDateTime] >= convert(datetime, '21/02/2015')  
AND [Extent1].[TxnDateTime] <= convert(datetime, '21/02/2017')

With datetime:

CPU 时间 = 1234 毫秒,运行时间 = 1250 毫秒。

With datetime2:

CPU 时间 = 1625 毫秒,运行时间 = 1645 毫秒。

据我了解,.NETDateTime类型映射到 SQL Serverdatetime2。我有什么选择呢?

该列可以为空datetime我将其与DateTime?


嗯,有趣!当我使用带有以下内容的表创建一个简单的数据库模型时DateTime列,实体框架默认创建一个 SQL DateTime 类型的列,而不是 DateTime2。我使用EntityFramework版本6.1.3

class Blog
{
     public int Id {get; set;}
     public DateTime IntroductionDate {get; set;}
}

Sql Server Management Studio 报告该列IntrdductionDate有属性(datetime, not null).

不管怎样,不久前我遇到了一个问题,我希望每个 DateTime 都被建模为dateTime2代替datetime。我想你可以使用类似的方法来强制列使用datetime

class MyDbContext : DbContext
{
    DbSet<...> ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         // Configure that all used DateTime should be modeled datetime2:
         const string standardDateTimeType = "datetime2";
         modelBuilder.Properties<DateTime>()
             .Configure(p => p.HasColumnType(standardDateTimeType);
         ...
    }

您可以将其用于您想要建模的所有类型。例如,如果您想建模所有小数都具有相同的精度和小数位数:

byte standardDecimalPrecision = 19;
byte standardDecimalScale = 8;
modelBuilder.Properties<decimal>()
    .Configure(p => p.HasPrecision(standardDecimalPrecision, standardDecimalScale));

当然,您可以使用设置列的类型ColumnAttribute数据注释,但这会迫使您对每个 DateTime 执行此操作,并且可能会出现未来的类会忘记这一点的错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 DateTime 比较期间,如何强制实体框架使用 datetime 而不是 datetime2? 的相关文章

随机推荐