Datetime2' Error When Using Entity Framework in VS 2010 .Net 4.0

datetime2' error when using entity framework in VS 2010 .net 4.0

Entity framework handles all the dates as a Datetime2, so, if your fields in the database are Datetime, this could be a problem.
We had the same problem here, and from what we found, populating all the date fields and changing the datatype, are the most commom solutions

The version of SQL Server in use does not support datatype datetime2?

In addition to @Mithrandir answer validate that your database is running in compatibility level set to 100 (SQL 2008).

You don't have to use DATETIME2 in your database to get this error. This error happens usually once you add required (NOT NULL) DATETIME column to existing table and you don't set the value prior to saving the entity to database. In such case .NET will send default value which is 1.1.0001 and this value doesn't fit into DATETIME range. This (or something similar) will be source of your problem.

System.ArgumentException: The version of SQL Server in use does not support datatype 'datetime2

It turns out that Entity Framework 4 somehow got the idea to use SQL Server 2008. The fix was to edit the .edmx file in an XML editor and set the ProviderManifestToken="2005" instead of 2008. (You need to rebuild.)

Entity Framework 4 maps DateTimeOffset to SQL datetime in Visual Studio 2010

Try going the other way (DB->Model). It worked for Julie Lerman. It seems to me your manually-edited EDMX should also work if you qualify the DateTimeOffset with a namespace.

EF Code First Pre-filter datetime on insert / update

I ended up writing a helper function to pre-update datetime values.

public static class DateTimeSwitch
{
public static void DateTimeToSqlDateTime(this object obj)
{
Type objType = obj.GetType();

if (typeof(IEnumerable).IsAssignableFrom(objType))
{
IEnumerable enumerable = (IEnumerable)obj;
if (enumerable != null)
{
foreach (object c in enumerable)
{
if (c != null)
c.DateTimeToSqlDateTime();
}
}
}
else
{
PropertyInfo[] properties = objType.GetProperties();

foreach (PropertyInfo property in properties)
{
if (typeof(DateTime).IsAssignableFrom(property.PropertyType))
{
// Get the value, adjust it.
DateTime value = (DateTime)property.GetValue(obj, null);
if (value < (DateTime)SqlDateTime.MinValue)
{
property.SetValue(obj, (DateTime)SqlDateTime.MinValue, null);
}
}
else if (!property.PropertyType.IsPrimitive && typeof(String) != property.PropertyType && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
IEnumerable enumerable = (IEnumerable)property.GetValue(obj, null);
if (enumerable != null)
{
foreach (object c in enumerable)
{
if (c != null)
c.DateTimeToSqlDateTime();
}
}
}
else if (!property.PropertyType.IsPrimitive)
{
if (property.PropertyType.Assembly == objType.Assembly)
{
var value = property.GetValue(obj, null);
if (value != null) value.DateTimeToSqlDateTime();
}
}
}
}
}
}


Related Topics



Leave a reply



Submit