强制DateTime与实体框架数据库首先 [英] Force DateTime with Entity Framework Database First

查看:148
本文介绍了强制DateTime与实体框架数据库首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我更新记录时,我的DateTimes被写入SQL类型DateTime2。生产服务器是SQL Server 2005(没有评论),所以更新失败,因为不支持DateTime2。



我已经尝试为我的模型创建一个元数据类根据 http://www.asp。 net / mvc / overview / get-started / database-first-development / enhancement-data-validation ,并用[Column(DbType =datetime)]装饰成员,但是我没有遵循说明



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



谢谢

解决方案

令人痛心的是Felippe的建议没有奏效,虽然我看不出有什么理由不应该。



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



首先我创建了这个课程:

 内部密封类MyCommandInterceptor:IDbCommandInterceptor 
{
public void NonQueryExecuted(DbCommand命令,DbCommandInterceptionContext< int> interceptionContext)
{
}

public void NonQueryExecuting DbCommand命令,DbCommandInterceptionContext< int& interceptionContext)
{
ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(command);
}

public void ReaderExecuted(DbCommand命令,DbCommandInterceptionContext< DbDataReader> interceptionContext)
{
}

public void ReaderExecuting(DbCommand command ,DbCommandInterceptionContext< DbDataReader> interceptionContext)
{
ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(command);
}

public void ScalarExecuted(DbCommand命令,DbCommandInterceptionContext< object> interceptionContext)
{
}

public void ScalarExecuting(DbCommand命令,DbCommandInterceptionContext< object> interceptionContext)
{
ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(command);
}

///< summary>
///将类型datetime2的参数更改为datetime,用于SQL Server2005兼容性。
///< / summary>
///< param name =command>命令< / param>
private void ChangeDateTime2ToDateTimeForSqlServer2005Compatibility(DbCommand命令)
{
foreach(DbParameter param in command.Parameters)
{
if(param.DbType == System.Data.DbType .DateTime2)
{
param.DbType = System.Data.DbType.DateTime;
}
}
}
}

然后我使用DbContext派生类中的静态构造函数激活它:

  static MyDbContext()
{
//命令拦截器将DateTime2数据类型更改为DateTime,用于SQL Server 2005的兼容性。
DbInterception.Add(new MyCommandInterceptor());
}

我要保持一个很好的观察,以确保它然而,行为。


I am connecting to a database using EF database first. Therefore my models are autogenerated.

When I update a record my DateTimes are getting written as SQL type DateTime2. The production server is SQL Server 2005 (no comments please), so the updates fail as DateTime2 is not supported.

I've tried creating a metadata class for my model as per http://www.asp.net/mvc/overview/getting-started/database-first-development/enhancing-data-validation and decorating the member with [Column(DbType = "datetime")], but either I'm not following the instructions properly or I'm on the wrong track.

How can I get EF database first to use DateTime when writing to the database?

Thanks

解决方案

Annoyingly Felippe's suggestion didn't work, although I can't see any reason why it shouldn't.

I managed to hack out my own solution. Not completely happy with it, but we're trying to get the client to upgrade to SQL Server 2014 so it isn't permanent. Comments welcome...

First I created this class:

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;
            }
        }
    }
}

Then I activate it using a static constructor in my DbContext derived class:

    static MyDbContext()
    {
        // The command interceptor changes DateTime2 data types to DateTime for SQL Server 2005 compatibility.
        DbInterception.Add(new MyCommandInterceptor());
    }

I'm going to keep a good watch on this to make sure that it behaves however.

这篇关于强制DateTime与实体框架数据库首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆