Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Issue: UpForGrabs: Query: Leap year bug on CreateDateTime and CreateDateTimeOffset cannonical functions [193]

$
0
0
"When you write something like this:
var d = db.Ps.DefaultIfEmpty().Select(p => EntityFunctions.CreateDateTime(2012,02,29,0,0,0));

You’ll get an exception saying:
An error occurred while reading from the store provider's data reader. See the inner exception for details.

Inner exception says:
Conversion failed when converting date and/or time from character string.

The problem is in the translation:

SELECT DATEADD(year, 2012 - 1, convert (datetime2,'0001' + '-' + convert(varchar(255), 2) + '-' + convert(varchar(255), 29) + ' ' + convert(varchar(255), 0) + ':' + convert(varchar(255), 0) + ':' + str(cast(0 as float(53)), 10, 7), 121)) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN [dbo].[P] AS [Extent1] ON 1 = 1


'0001-2-29' is an invalid date because year 1 is not a leap year. I have no idea why we translate to this awkward SQL.



Workaround:

Use this instead in LINQ:
new DateTime(2012,02,29)

Which translates into:
convert(datetime2, '2012-02-29 00:00:00.0000000', 121)"

This item was migrated from the DevDiv work item tracking system [ID=376040].


Viewing all articles
Browse latest Browse all 10318

Trending Articles