Short Answer This can happen if you do not initialize a value to a DateTime field; the field does not accept NULL values, and it's a value type, so the default value of the non-nullable DateTime type will be used. Setting the value fixed it for me!
The DateTime2 is an SQL Server data type, that stores both date & time together. The time is based on the 24 hours clock. The DateTime2 stores the fractional seconds Up to 7 decimal places (1⁄10000000 of a second). The Precision is optional and you can specify it while defining the DateTime2 column.
The main difference is the way of data storage: while in Datetime type, the date comes first and then time, in Datetime2, 3 bytes, in the end, represents date part!
DATETIME2 has a date range of "0001 / 01 / 01" through "9999 / 12 / 31" while the DATETIME type only supports year 1753-9999. Also, if you need to, DATETIME2 can be more precise in terms of time; DATETIME is limited to 3 1/3 milliseconds, while DATETIME2 can be accurate down to 100ns.
The issue is that you're using ApplyPropertyChanges
with a model object that has only been populated with data in the form (headline, story, and image). ApplyPropertyChanges
applies changes to all properties of the object, including your uninitialized DateTime
, which is set to 0001-01-01, which is outside of the range of SQL Server's DATETIME
.
Rather than using ApplyPropertyChanges
, I'd suggest retrieving the object being modified, change the specific fields your form edits, then saving the object with those modifications; that way, only changed fields are modified. Alternately, you can place hidden inputs in your page with the other fields populated, but that wouldn't be very friendly with concurrent edits.
Update:
Here's an untested sample of just updating some fields of your object (this is assuming you're using LINQ to SQL):
var story = _db.ArticleSet.First(a => a.storyId == ArticleToEdit.storyId);
story.headline = ArticleToEdit.headline;
story.story = ArticleToEdit.story;
story.image = ArticleToEdit.image;
story.modifiedDate = DateTime.Now;
_db.SubmitChanges();
This is a common error people face when using Entity Framework. This occurs when the entity associated with the table being saved has a mandatory datetime field and you do not set it with some value.
The default datetime object is created with a value of 01/01/1000
and will be used in place of null. This will be sent to the datetime column which can hold date values from 1753-01-01 00:00:00
onwards, but not before, leading to the out-of-range exception.
This error can be resolved by either modifying the database field to accept null or by initializing the field with a value.
DATETIME
supports 1753/1/1 to "eternity" (9999/12/31), whileDATETIME2
support 0001/1/1 through eternity.
Msdn
Answer:
I suppose you try to save DateTime
with '0001/1/1' value. Just set breakpoint and debug it, if so then replace DateTime
with null
or set normal date.
This one was driving me crazy. I wanted to avoid using a nullable date time (DateTime?
). I didn't have the option of using SQL 2008's datetime2 type either (modelBuilder.Entity<MyEntity>().Property(e => e.MyDateColumn).HasColumnType("datetime2");
).
I eventually opted for the following:
public class MyDb : DbContext
{
public override int SaveChanges()
{
UpdateDates();
return base.SaveChanges();
}
private void UpdateDates()
{
foreach (var change in ChangeTracker.Entries<MyEntityBaseClass>())
{
var values = change.CurrentValues;
foreach (var name in values.PropertyNames)
{
var value = values[name];
if (value is DateTime)
{
var date = (DateTime)value;
if (date < SqlDateTime.MinValue.Value)
{
values[name] = SqlDateTime.MinValue.Value;
}
else if (date > SqlDateTime.MaxValue.Value)
{
values[name] = SqlDateTime.MaxValue.Value;
}
}
}
}
}
}
You can also fix this problem by adding to model (Entity Framework version >= 5)
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreationDate { get; set; }
If you have a column that is datetime and allows null you will get this error. I recommend setting a value to pass to the object before .SaveChanges();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With