I have had this problem in the past as Entity Framework standard for DateTime is the DateTime2 field, the obviously workaround is to update all columns to datetime2 in your SQL Server database, however datetime2 data-type was only introduced in SQL Server 2008, so here are a few things to try out:
First: ensure that if the DateTime field in your SQL Database is nullable, your model is using "DateTime?" (nullable datetime) instead of "DateTime".
Second: open your EDMX file with any XML editor (Visual Studio should work) and change the value of your ProviderManifestToken
to ProviderManifestToken="2005"
, this should ensure SQL Server 2005 compatibility.
That works with .edmx files, but with Code First changing will be a little bit more challenging and will depend on your Entity Framework Version, so Microsoft recommends trying to specify your column type on your OnModelCreating method, like the following example:
modelBuilder.Entity<Blog>().Property(t => t.CreatedOn).HasColumnName("CreatedOn").HasColumnType("date");
Try experimenting with values for ColumnType until you reach your objective.
If you still want to change your ProviderManifestToken
value:
E.F. 6: you can create a configuration for your DbContext, basically a class like this:
/// <summary>
/// A configuration class for SQL Server that specifies SQL 2005 compatability.
/// </summary>
internal sealed class EntityFrameworkDbConfiguration : DbConfiguration
{
/// <summary>
/// The provider manifest token to use for SQL Server.
/// </summary>
private const string SqlServerManifestToken = @"2005";
/// <summary>
/// Initializes a new instance of the <see cref="EntityFrameworkDbConfiguration"/> class.
/// </summary>
public EntityFrameworkDbConfiguration()
{
this.AddDependencyResolver(new SingletonDependencyResolver<IManifestTokenResolver>(new ManifestTokenService()));
}
/// <inheritdoc />
private sealed class ManifestTokenService : IManifestTokenResolver
{
/// <summary>
/// The default token resolver.
/// </summary>
private static readonly IManifestTokenResolver DefaultManifestTokenResolver = new DefaultManifestTokenResolver();
/// <inheritdoc />
public string ResolveManifestToken(DbConnection connection)
{
if (connection is SqlConnection)
{
return SqlServerManifestToken;
}
return DefaultManifestTokenResolver.ResolveManifestToken(connection);
}
}
}
Usage:
DbConfigurationType(typeof(EntityFrameworkDbConfiguration))]
public class MyContextContext : DbContext
{
}
(source: How to configure ProviderManifestToken for EF Code First)
E.F. 5 and older: Read this post that easily clarifies it: http://blog.oneunicorn.com/2012/04/21/code-first-building-blocks/
Hope that helps.