Reputation: 8461
I'm using EF to save a date time.
The Date is being saved to the database but not the time. I'm using the date
type. However, the SSMS (2012 express) field only shows dd/MM/yyyy
(no time)
Reading it back it reads something like 26/07/2014 00:00:00
I'm assuming it's showing 00:00:00 because that is how it is being saved (despite not showing).
To save the entry, I'm just assigning a DateTime to my property.
myEfModel.Date = DateTime.Now;
I also tried (to ensure the number of digits wasn't confusing SQL on the save)
var timeNow = DateTime.Now;
timeNow.AddMilliseconds(-timeNow.Millisecond);
myEfModel.Date LastUsed = timeNow;
Searches on SO shows most people claiming they have no problems saving DateTime.Now via EF
Why won't the time save?
Upvotes: 2
Views: 3590
Reputation: 754518
The DATE
datatype in SQL Server 2012 specifically saves only the date (no time) - it's a feature not a bug.... (the DATE
datatype was introduced in SQL Server 2008 specifically to be used when you only care for the date, e.g. "hire date" or things like that, when the time portion usually just gets in your way).
See the MSDN documentation Date and Time Data Types in SQL Serer Books Online for more details....
If you need time, too, use DATETIME2(n)
in your database table instead (where the n
can have a value between 0 and 7 and defines how many fractional second decimal points you need; 0 = accuracy to the second, 7 = accuracy to 100ns)
Upvotes: 9