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
Change the Value in App.Config File Dynamically
Owin's Getexternallogininfoasync Always Returns Null
Improve Wpf Datagrid Performance
Run Selenium Tests in Multiple Browsers One After Another from C# Nunit
How to Add an Ampersand for a Value in a ASP.NET/C# App Config File Value
From Excel to Datatable in C# with Open Xml
How to Add Event Handler for Dynamically Created Controls at Runtime
Login to the Page with Httpwebrequest
Customattribute Reflects HTML Attribute MVC5
Ef Data Context - Async/Await & Multithreading
Dynamic Lang. Runtime VS Reflection
Handling Multiple Requests with C# Httplistener
C# Reflection - Load Assembly and Invoke a Method If It Exists