Reputation: 6455
Here's the snippet of my code.
var sc = dc.Schedules.FirstOrDefault(s => s.scheduleID == id);
schedule = new ScheduleModel
{
ScheduleId = sc.scheduleID,
Name = sc.scheduleName,
StartDate = sc.startDate.HasValue ? sc.startDate.Value : default(DateTime),
EndDate = sc.endDate.HasValue ? sc.endDate.Value : default(DateTime),
StatusId = sc.statusID.HasValue ? sc.statusID.Value : default(int),
//other properties
};
The original code didn't check sc.startDate.HasValue
, and Resharper complained about Possible System.InvalidOperationException
warning.
Then I added these conditions so that Resharper didn't complain anymore. But I was also wondering if it's right to use default(DateTime) & defualt(int)
here. Would SQL Server accept its default DateTime
value? Or should I perhaps use DateTime.Now
instead?
Any suggestions? Thanks.
Upvotes: 0
Views: 301
Reputation: 223267
The default value of DateTime
is 1/1/0001 12:00:00 AM
whereas for SQL Server the DateTime field range is January 1, 1753, through December 31, 9999
, so its better if you use NULL
instead of default value, as you will not be able to store the .Net framework default value for date in SQL Server.
For your class ScheduleModel
, I would keep StartDate
and EndDate
as DateTime?
or Nullable<DateTime>
Upvotes: 4