Entity framework handles all the dates as a Datetime2, so, if you fields in the database are Datetime, this could be a problem. We had the same problem here, and from what we found, populating all the date fields and changing the datatype, are the most commom solutions
+4
A:
Tejo
2010-08-27 17:16:35
I was a bonehead. I didn't populate all my date fields (apparently I had 2)
KevinDeus
2010-08-27 17:18:01
I can't judge you, our case was exaclty the same... even the solution
Tejo
2010-08-27 17:27:03
Regarding EF - wouldn't it make more sense that it used the Date type that is used by the database table it is mapped to instead of arbitrarily assuming everyone should be using DateTime2? And really DateTime2?? - they couldn't just upgrade DateTime they had to create a new datatype and the ingeniously added a 2 to it?
Jeremy Coenen
2010-09-16 19:15:46
A:
Whatever fits in a datetime will fit in a datetime2 data type, vice versa this is not the case, you can stick a date of January 1500 in a datetime2 data type but datetime only goes back to 1753, a datetime2 column can go back all the way to the year 1. I would check what the min date that you are passing in is and if your tables have datetime2 or datetime data type columns
SQLMenace
2010-08-27 17:17:15
A:
Use that SQL script to convert all the columns from datetime to datetime2. It skips all the tables contains 'aspnet' for your convinience.
DECLARE @SQL AS NVARCHAR(1024)
DECLARE @TBL AS NVARCHAR(255)
DECLARE @COL AS NVARCHAR(255)
DECLARE @NUL AS BIT
DECLARE CUR CURSOR FAST_FORWARD FOR
SELECT t.name, c.name, c.is_nullable
FROM sys.tables AS t
JOIN sys.columns c ON t.object_id = c.object_id
JOIN information_schema.columns i ON i.TABLE_NAME = t.name AND i.COLUMN_NAME = c.name
WHERE i.data_type = 'datetime' and t.name not like '%aspnet%'
ORDER BY t.name, c.name
OPEN CUR
FETCH NEXT FROM CUR INTO @TBL, @COL, @NUL
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL = 'ALTER TABLE ' + @TBL + ' ALTER COLUMN ' + @COL + ' datetime2' + (CASE WHEN @NUL=1 THEN '' ELSE ' NOT' END) + ' NULL;'
EXEC sp_executesql @SQL
FETCH NEXT FROM CUR INTO @TBL, @COL, @NUL
END
CLOSE CUR;
DEALLOCATE CUR;
It works for me!