Home > Articles > Details

Asp.Net Core 3.1 Entity Framework Core DateTime field getting updated to 1/1/0001

 Question: How do you resolve the datetime that results to 1/1/0001 and when I try to changes the data type in the database the following error happens: "Unable to modify table.  
The conversion of a date data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated."


Please Login to see the rest of the answer
Answer:
In your Asp.Net Core 3.1 application DatabaseContext, find where you have defined the DateTime Property and Add (.ValueGeneratedOnAdd();)
- Entity Framework sometimes does not know whether to update or result to default the Data Type if not properly typed in the Database Context. See the code below:

modelBuilder.Entity<ModelClassName>(entity =>
            {
                entity.ToTable("NameOfTableInDatabase");

                entity.Property(e => e.id);

                entity.Property(e => e.fName);
                entity.Property<DateTime>(e => e.dateCreated).ValueGeneratedOnAdd();

                entity.Property<DateTime>(e => e.anotherDate).ValueGeneratedOnAdd();
         });

 


If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy