15

I'm using EF Code first with my asp.net mvc application. here is my code:

Request.RequestDate = DateTime.Now;

the type of RequestDate is datetime in my database. and this is error that occurred when i use the above code!:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

please help me. thanks.

4
  • possible duplicate of c# conversion of a datetime2 data type to a datetime data type
    – CodeCaster
    May 14, 2012 at 14:26
  • Are you using SQL 2005 ? datetime2 range is from 0001 / 01 / 01 through to 9999 / 12 / 31 hence the error. If so, you'll need to edit the XML edmx file
    – Jason Jong
    May 14, 2012 at 14:26
  • @json jong: I'm using sql server 2008 and i have no .edmx file because i use Code first!. May 14, 2012 at 14:31
  • Can't be answered without seeing the class model and the (full) code throwing the exception. Sep 22, 2022 at 9:22

3 Answers 3

13

Edit:

How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

The issue is that you are trying to save a value that cannot fit in a SQL datetime column. the answer givin here will stop the conversion from failing.

or in your Database change the columns to be type datetime2. I don't exactly know why they code first is generating datetime columns instead of datetime2

Here is an example to explicitly map your Datetime columns to datetime2 in SQL

Using DateTime properties in Code-First Entity Framework and SQL Server

4
  • I'm using Code First and i have no .edmx file. May 14, 2012 at 14:29
  • I Think what you need to do is specify the column is of type datetime. Did your code first generate the Database? your trying to store a datetime2 object in a datetime column
    – Blast_dan
    May 14, 2012 at 14:36
  • Yes my database generated successfully with code first and that column is of type datetime. May 14, 2012 at 14:40
  • What SQL database are you using?
    – Blast_dan
    May 14, 2012 at 14:41
9

On my side it was because the datetime was not nullable and in some situation the field was not initialized in the constructor. I solved this by setting my field as nullable

public DateTime? MyDate { get; set; }

Another solution would be to initialize the field in the constructor no matter what.

1
  • This solved it for me. On reflection the error message isn't great. I created a DateTime column, why is it trying to convert it to something else? why not just tell me a constraint has been violated...
    – sarin
    Feb 22, 2016 at 10:11
0
    --in your sql script:
        create table tablename(mydate DateTime not null default GETDATE())


        //in your c# object:
        public class tablename
        {
          public string mydate {get; set; } = null;
        }

        //in your C# method using EF:
        private async Task mymethod()
        {
            DateTime myDateTime = DateTime.Now;
            string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
            dbContext.tablename.add(new tablenam{mydate = sqlFormattedDate });
            await dbContext.SaveChangesAsync();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.