22

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

my code is like this:

        using (var contxt = new realtydbEntities())
        {
            var status = GetStatus();

            var repIssue = new RepairIssue()
            {
                CreaterId = AuthorId,
                RepairItemDesc = this.txtDescription.Text,
                CreateDate = DateTime.Now,//here's the problem
                RepairIssueStatu = status
            };

            contxt.AddObject("RepairIssues", repIssue);
            contxt.SaveChanges();
        }

the CreateDate property mapping to a column which type is smalldatetime.

how to make this code run?

Scott 混合理论
  • 2,263
  • 8
  • 34
  • 59
  • Possible duplicate of [The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range?](https://stackoverflow.com/questions/7386360/the-conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-o) – Tot Zam Aug 18 '17 at 14:58

6 Answers6

22

I had the same exception, but it was because a non nullable datetime property that taking the min datetime value. That wasn't a smalldatetime at DB, but the min datetime of C# exceed the limit of min datetime of SQL. The solution was obvious, set the datetime properly. BTW, the code wasn't mine, and that's why I wasn't aware of that property :)

Milton
  • 928
  • 1
  • 10
  • 22
11

The root of your problem is that the C# DateTime object is "bigger" than SQL's smalldatetime type. Here's a good overview of the differences: http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes

So really your options are:

  1. Change the column type from smalldatetime to datetime (or datetime2)
  2. Instead of using EF, construct your own SQL Command (and you can use SqlDateTime)
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
Jason
  • 4,232
  • 3
  • 23
  • 31
  • 3
    This does not explain why `DateTime.Now will` not fit into a SmallDateTime. It is not so much about the size, but about the range and precision/accuracy that it can store. `DateTime.Now` should always be a valid value for a SmallDateTime field (unless you set your PC clock back 300 years), however I am currently experiencing exactly the same error while trying to simply save a 'DateTime.Now' value of {04/10/2013 12:49:00} into a SQL SmallDateTime field. It is frustrating that I cannot find a correct answer anywhere on SO. – iCollect.it Ltd Oct 04 '13 at 12:00
  • @TrueBlueAussie An automatic conversion from a "larger" type to a "smaller" type is disallowed because information could be lost in the transformation. This is equally true whether "larger" means "can hold bigger numbers" or whether it means "can hold more precision". C# assumes that those fractional seconds are important, unless you specifically say that they are not (for example, with an explicit cast). – GrandOpener Dec 17 '13 at 20:56
  • @GrandOpener: Sorry you misunderstood my comment. I simply meant the answer *should have explained why dateTime.Now would not fit into a SmallDateTime*. I was not asking for an explanation :) – iCollect.it Ltd Dec 23 '13 at 14:23
3

Add this to your model class:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));
    }
1

SqlDateTime will allow you to do what you need.

Rob
  • 1,071
  • 8
  • 10
  • If you create a `SqlDateTime` from the `DateTime` as you suggest, then use its `Value` for the EF update (as the EF property is a DateTime) you still get exactly the same error (if you have this error). This answer appears incorrect for EF (which the question is about) unless you can clarify how this solves the problem. – iCollect.it Ltd Oct 04 '13 at 12:06
0

I got this error because I had added a datetime column to my SQL table and application WITHOUT removing the old data. I found that I could update new records; but, the records that were in the table prior to the added field threw this error when an update was attempted on one of them.

midohioboarder
  • 438
  • 5
  • 14
0

check your migration content. I changed my model like this"

public DateTime? CreationDate { get; set; }

then add new migration and finally run update database

Ghadir Farzaneh
  • 429
  • 5
  • 6