SqlDateTime overflow error while saving DateTime.MinValue in POCO object

I am having som problems while saving POCO objects using DateTime property. When the DateTime property is DateTime.MinValue, SaveChanges () fails due to the difference between SqlDateTime.MinValue and DateTime.MinValue.

So what to do?

1) Should I check DateTime.MinValue before saving the object?

2) Should I use the POCO property in datetime, something like this?

private SqlDateTime _created; public virtual DateTime Created { get { return _created.Value; } set { _created = value == DateTime.MinValue ? SqlDateTime.MinValue : value; } } 

/ Pw

+6
entity entity-framework datetime2 poco
source share
2 answers

If possible, I would recommend setting the database field to null and setting the value to null, not min.

As an alternative, I would develop a property as follows:

 private SqlDateTime? _created; public virtual DateTime Created { get { return (DateTime)(_created ?? SqlDateTime.MinValue); } set { if (value == null || value < (DateTime)SqlDateTime.MinValue) { _created = SqlDateTime.MinValue; } else { _created = (SqlDateTime)value; } } } 
+5
source share

The easiest approach I can imagine is to initialize the DateTime properties for (DateTime)SqlDateTime.MinValue :

 public class SomeEntity { public SomeEntity() { Updated = (DateTime)SqlDateTime.MinValue; } public DateTime Updated { get; set; } } 
+4
source share

All Articles