0

I'm trying to insert only Date and Card into my database table and I'm getting the below error

Error Exception {"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}

Here is my table structure

enter image description here

Here is my class for TimeOutJustification

    public class TimeoutJustification
    {
        public long ID { get; set; }
        public DateTime Date { get; set; }
        public string Card { get; set; }
        public DateTime TimeIn { get; set; }
        public DateTime TimeOut { get; set; }
        public long TotalTime { get; set; }
        [NotMapped]
        public TimeSpan TotalTimeSpan
        {
            get
            {
                return new TimeSpan(TotalTime);
            }
            set
            {
                TotalTime = value.Ticks;
            }
        }
        public string Justification { get; set; }

        public string Details { get; set; }

        public string GeneralJustification { get; set; } 

    }
}

An error exception The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated." get caught whenever I'm saving into database.

Hence I'm using entity framework and my ID in my database is auto increment

Here is an example of how it looks like

List<TimeoutJustification>withOutTimeOuts;

withOutTimeOuts.Add(new TimeOutJustification{Card="1234",Date="12-03-19"}

db.JustificationTable.AddRange(withOutTimeOuts);
//Error Gets Caught here
db.SaveChanges();
Sam
  • 25
  • 8

1 Answers1

0

datetime2 coveres a larger range than datetime.

datetime2:

Date range 0001-01-01 through 9999-12-31

datetime:

Date range January 1, 1753, through December 31, 9999

Date="12-03-19" probably converts to year 12 (or 19). The solution could be using DateTime for the Date property:

public class TimeOutJustification
{
    public DateTime Date { get; set; }
    ...
}
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63