-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
When comparing against the DateTimeOffset.Date property, we're seeing inconsistent results on client side vs server side evaluation.
Steps to reproduce
Given the entity:
public partial class PreHire
{
public int PreHireId { get; set; }
public DateTimeOffset StartDate { get; set; }
}And the following logic:
DateTime startDate = DateTime.Now;
//Pulls preHire with a start date of '2019-11-25 14:42:54.0833333 +00:00'
PreHire clientComparePreHire = context.PreHire.Where(x => x.PreHireId == 22407).FirstOrDefault();
if(clientComparePreHire.StartDate.Date >= startDate.Date)
{
Console.WriteLine("This works.");
}
List<PreHire> serverComparePreHires = context.PreHire.Where(q => q.StartDate.Date >= startDate.Date).ToList();
if(serverComparePreHires.Count == 0)
{
Console.WriteLine("This doesn't. This shouldn't be empty.");
}We get the following logs:
info: Microsoft.EntityFrameworkCore.Database.Command[20100]
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [p].[PreHireID], [p].[StartDate]
FROM [TBL].[PreHire] AS [p]
WHERE [p].[PreHireID] = 22407
This works.
info: Microsoft.EntityFrameworkCore.Database.Command[20100]
Executing DbCommand [Parameters=[@__startDate_Date_0='2019-11-25T00:00:00' (DbType = DateTimeOffset)], CommandType='Text', CommandTimeout='30']
SELECT [p].[PreHireID], [p].[StartDate]
FROM [TBL].[PreHire] AS [p]
WHERE CONVERT(date, [p].[StartDate]) >= @__startDate_Date_0
This doesn't. This shouldn't be empty.
This shows that serverComparePreHires is empty, but if I run the above query manually I get my expected result:
SELECT [p].[PreHireID], [p].[StartDate]
FROM [TBL].[PreHire] AS [p]
WHERE CONVERT(date, [p].[StartDate]) >= '2019-11-25T00:00:00'
PreHireID StartDate
22407 2019-11-25 14:42:54.0833333 +00:00
To summarize, When processing this specific linq query server side, EF Core generates a valid SQL Query but fails to return the matching PreHire entry.
Further technical details
EF Core version: 3.0.0 (Same result with 3.0.1)
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0 (Migrating from 2.1)
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.7
maxima120CodeSwimBikeRunner