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."


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();
         });

 






© 2024 - ErnesTech - Privacy
E-Commerce Return Policy