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

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

 


Edit this Article
DropDownList Razor C MVC using Asp.Net Core 3.1 Technology published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

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

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy