Skip to content

Instantly share code, notes, and snippets.

@eddie3716
Created May 8, 2019 22:00
Show Gist options
  • Save eddie3716/d6e4304c6465cfa255fb68bf4993d861 to your computer and use it in GitHub Desktop.
Save eddie3716/d6e4304c6465cfa255fb68bf4993d861 to your computer and use it in GitHub Desktop.
Entity Framework 6 SQL Server 2005 Compatibility
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class SqlServer2005CommandInterceptor : IDbCommandInterceptor
{
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(command);
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(command);
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(command);
}
/// <summary>
/// Changes parameters of type datetime2 to datetime for SQL server2005 compatibility.
/// </summary>
/// <param name="command">The command.</param>
private void ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(DbCommand command)
{
if (command.CommandText.Contains("datetime2"))
{
command.CommandText = command.CommandText.Replace("datetime2", "datetime");
}
foreach (DbParameter param in command.Parameters)
{
if (param.DbType == System.Data.DbType.DateTime2)
{
param.DbType = System.Data.DbType.DateTime;
}
}
}
}
}
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Infrastructure.DependencyResolution;
using System.Data.Entity.Infrastructure.Interception;
namespace Data
{
public class SqlServer2005ShimDbConfiguration : DbConfiguration
{
static SqlServer2005ShimDbConfiguration()
{
// The command interceptor changes DateTime2 data types to DateTime for SQL Server 2005 compatibility.
DbInterception.Add(new SqlServer2005CommandInterceptor());
}
/// <summary>
/// The provider manifest token to use for SQL Server....trick Entity framework into thinking
/// this is SQL Server 2008, or else it will throw an error saying the datetime2 data type
/// isn't supported in our version of sql.
/// The folks managing the EF6 and EF Core projects are really opinionated about their
/// love for datetime2, and loathing for datetime.
///
/// If there ever comes a time when we finally upgrade out of SQL Server 2005, then this whole shim can be deleted.
/// </summary>
private const string SqlServerManifestToken = @"2008";
/// <summary>
/// Initializes a new instance of the <see cref="SqlServer2005ShimDbConfiguration"/> class.
/// </summary>
public SqlServer2005ShimDbConfiguration()
{
this.AddDependencyResolver(new SingletonDependencyResolver<IManifestTokenResolver>(new ManifestTokenService()));
}
/// <inheritdoc />
private sealed class ManifestTokenService : IManifestTokenResolver
{
/// <inheritdoc />
public string ResolveManifestToken(DbConnection connection)
{
return SqlServerManifestToken;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment