Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 首先使用实体框架数据库强制DateTime_Sql Server_Entity Framework_Datetime_Ef Database First_Datetime2 - Fatal编程技术网

Sql server 首先使用实体框架数据库强制DateTime

Sql server 首先使用实体框架数据库强制DateTime,sql-server,entity-framework,datetime,ef-database-first,datetime2,Sql Server,Entity Framework,Datetime,Ef Database First,Datetime2,我首先使用EF数据库连接到数据库。因此,我的模型是自动生成的 当我更新一条记录时,我的日期时间被写入SQL类型DateTime2。生产服务器是SQL server 2005(请不要评论),因此更新失败,因为不支持DateTime2 我已尝试根据为模型创建元数据类,并使用[Column(DbType=“datetime”)]装饰成员,但要么我没有正确遵循说明,要么我走错了方向 如何让EF数据库在写入数据库时首先使用DateTime 感谢我以前遇到过这个问题,因为DateTime的实体框架标准是Da

我首先使用EF数据库连接到数据库。因此,我的模型是自动生成的

当我更新一条记录时,我的日期时间被写入SQL类型DateTime2。生产服务器是SQL server 2005(请不要评论),因此更新失败,因为不支持DateTime2

我已尝试根据为模型创建元数据类,并使用[Column(DbType=“datetime”)]装饰成员,但要么我没有正确遵循说明,要么我走错了方向

如何让EF数据库在写入数据库时首先使用DateTime


感谢

我以前遇到过这个问题,因为DateTime的实体框架标准是DateTime2字段,很明显,解决方法是将SQL Server数据库中的所有列更新为DateTime2,但是DateTime2数据类型仅在SQL Server 2008中引入,因此这里有一些东西可以尝试:

第一:确保如果SQL数据库中的DateTime字段可为空,则模型使用的是“DateTime?”(可为空的DateTime)而不是“DateTime”

第二:使用任何XML编辑器打开您的EDMX文件(Visual Studio应该可以工作),并将
ProviderManifestToken
的值更改为
ProviderManifestToken=“2005”
,这将确保SQL Server 2005的兼容性

这适用于.edmx文件,但对于“代码优先”更改将有点困难,并且取决于实体框架版本,因此Microsoft建议尝试在OnModelCreating方法中指定列类型,如以下示例所示:

modelBuilder.Entity<Blog>().Property(t => t.CreatedOn).HasColumnName("CreatedOn").HasColumnType("date");
用法:

DbConfigurationType(typeof(EntityFrameworkDbConfiguration))]
public class MyContextContext : DbContext
{
}
(来源:)

E.F.5及以上:阅读这篇容易澄清的帖子:


希望这能有所帮助。

令人恼火的是,费利佩的建议不起作用,尽管我看不出有什么理由不起作用

我设法想出了自己的解决办法。不完全满意,但我们正在尝试让客户端升级到SQL Server 2014,因此它不是永久性的。欢迎评论

首先,我创建了这个类:

internal sealed class MyCommandInterceptor : 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)
    {
        foreach (DbParameter param in command.Parameters)
        {
            if (param.DbType == System.Data.DbType.DateTime2)
            {
                param.DbType = System.Data.DbType.DateTime;
            }
        }
    }
}

我将对此保持密切关注,以确保其正常运行。

我通过删除数据库来解决此问题,并在package manager控制台中再次运行update database命令。但不适用于生产环境。

应注意,下次验证模型时,直接编辑
edmx
文件将被覆盖。所以这不应该被使用。我喜欢使用
DbConfiguration
的方法,这看起来很有希望。不幸的是,自定义DbConfiguration类似乎被完全忽略了。我正在使用EF 6.1.3David,您是否尝试在OnModelCreating方法上指定列类型?
internal sealed class MyCommandInterceptor : 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)
    {
        foreach (DbParameter param in command.Parameters)
        {
            if (param.DbType == System.Data.DbType.DateTime2)
            {
                param.DbType = System.Data.DbType.DateTime;
            }
        }
    }
}
    static MyDbContext()
    {
        // The command interceptor changes DateTime2 data types to DateTime for SQL Server 2005 compatibility.
        DbInterception.Add(new MyCommandInterceptor());
    }