conversion of a datetime2 data type to a smalldatetime EF6 MVC5

asp.net-mvc-5 entity-framework-6 sql type-conversion

Question

I'm starting with EF6 code and MVC5. In both my tables (Kommunication and Itemannounce), the Date(Datum) column is of the smalldatetime type, and when I attempt to change the date using System.DateTime, it fails. I now encounter the following mistake in the column:

The conversion of a datetime2 data type to a smalldatetime data type resulted in an out-of-range value.The statement has been terminated.

Since I am aware of byte sizes, I attempted the following fix in my situation:

  protected override void OnModelCreating( DbModelBuilder modelBuilder )
        {
        var annons = modelBuilder.Entity<Itemannounce> ( );
        annons.Property ( f => f.Datum ).HasColumnType ( "smalldatetime" );
        var komm = modelBuilder.Entity<Kommunication> ( );
        komm.Property ( f => f.Date ).HasColumnType ( "smalldatetime" );
        base.OnModelCreating ( modelBuilder );
        }

Still get the same problem. Without success, I attempted to set the columns to NOT NULL. I have been stuck for a few days and have not been able to locate a solution. Does anybody have a fix that I haven't thought of?

1
0
5/13/2017 12:23:32 AM

Popular Answer

The smalldatetime minimum value is1900-01-01

Datetime2's minimum value is0000-01-01

We are presumably providing a datetime2 value into a smalldatetime column in this case. Datetime2 defaults to its bare minimum value if it is not specified and is NON NULL.0000-01-01 . This is well below smalldatetime's floor. It is not permitted. You would get the error notice you are seeing if you entered a value that was outside of smalldatetime's acceptable range.

The conversion of a datetime2 data type to a smalldatetime data type resulted in an out-of-range value.The statement has been terminated.

There are several solutions to this. I propose modifying the model to make advantage of datetime2.

 protected override void OnModelCreating( DbModelBuilder modelBuilder )
        {
        var annons = modelBuilder.Entity<Itemannounce> ( );
        annons.Property ( f => f.Datum ).HasColumnType ( "datetime2" );
        var komm = modelBuilder.Entity<Kommunication> ( );
        komm.Property ( f => f.Date ).HasColumnType ( "datetime2" );
        base.OnModelCreating ( modelBuilder );
        }

If you don't want to alter the model, NULL checking may also be effective. You should have some thoughts after This related query.

0
5/23/2017 11:47:23 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow