How do I force Entity Framework to use datetime instead of datetime2, during DateTime comparison?

Viewed 3302

I am using Entity Framework for retrieving records and one of the filters is by datetime.

It generates a query like this:

([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))

Is there a way I can make EF convert to datetime instead of datetime2? It seems much faster.

I want EF to generate this:

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

With datetime:

CPU time = 1234 ms, elapsed time = 1250 ms.

With datetime2:

CPU time = 1625 ms, elapsed time = 1645 ms.

I understand that the .NET DateTime type maps to SQL Server datetime2. What are my options though?

The column is nullable datetime and I am comparing it to DateTime?

1 Answers
Related