I\'m using the DbContext and Code First APIs introduced with Entity Framework 4.1.
The data model uses basic data types such as string
In some cases, DateTime.MinValue (or equivalenly, default(DateTime)) is used to indicate an unknown value.
This simple extension method can help handle such situations:
public static class DbDateHelper
{
///
/// Replaces any date before 01.01.1753 with a Nullable of
/// DateTime with a value of null.
///
/// Date to check
/// Input date if valid in the DB, or Null if date is
/// too early to be DB compatible.
public static DateTime? ToNullIfTooEarlyForDb(this DateTime date)
{
return (date >= (DateTime) SqlDateTime.MinValue) ? date : (DateTime?)null;
}
}
Usage:
DateTime? dateToPassOnToDb = tooEarlyDate.ToNullIfTooEarlyForDb();