ef6 icon indicating copy to clipboard operation
ef6 copied to clipboard

Query sent to database are missing milliseconds in datetime

Open mattiaskagstrom opened this issue 6 years ago • 3 comments

When doing a .Where on a table with a Datetime2 column datetimes are sent without milliseconds and does therefor not match the intended rows.

Steps to reproduce

var millisecondDate = new Datetime("2019-09-06 09:48:49.613");
ctx.exampleTable.Where(row => row.date == millisecondDate).First();

generates: image

-- p__linq__1: '2019-09-06 09:48:49' (Type = DateTime2, IsNullable = false)

Further technical details

EF version: 6.4.4 Database Provider: EntityFramework.SqlServer Operating system: Windows 10 IDE: Visual Studio 2019 16.6.5

mattiaskagstrom avatar Aug 13 '20 15:08 mattiaskagstrom

Found this answer which seems to be a working workaround: https://stackoverflow.com/questions/3567734/how-to-compare-date-with-milliseconds-with-linq

if this bug is from 2010 and still in EF I have no hope of this getting resolved?

mattiaskagstrom avatar Aug 14 '20 06:08 mattiaskagstrom

Ran into this issue moving SQL Server database from Compatibility Level 100 to 150. The above solution compiled, but threw an exception that Date is not supported for EF 6.4.4. Resolved issue by creating a stored procedure with a datetime parameter. Imported into the EF model worked.

C# Code: var list = context.usp_GetOrderTransactions(datePosted).ToList();

Stored Procedure:

CREATE PROCEDURE usp_GetOrderTransactions
	@posted datetime
AS
BEGIN
	SELECT *
	FROM OrderTransaction
	WHERE CreatedDateTime = @posted;
END

Stored Procedure simplified for demonstration purposes.

garyfuhr avatar Dec 11 '21 10:12 garyfuhr

This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.

ajcvickers avatar Apr 25 '22 09:04 ajcvickers