NHibernate忽略长度属性 [英] NHibernate ignores length attribute

查看:64
本文介绍了NHibernate忽略长度属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行SQL Profiler之后,我意识到NHibernate正在将字符串映射到nvarchar(4000).我通过在hbm文件中指定type = AnsiString和length = ...来修复它.现在它正在生成varchar(8000)语句,并且忽略了长度.怎么会来!!

After running SQL Profiler, I realized that NHibernate was mapping strings to nvarchar(4000). I fixed it by specifying type=AnsiString and length=... in the hbm file. It is now generating varchar(8000) statements, and it is ignoring the length. How come?!

hbm文件:

<property name="EmailAddress" column="EMAIL_ADDRESS" type="AnsiString" length="120" />

数据库字段:

[EMAIL_ADDRESS] [varchar](120) NULL,

TIA

推荐答案

实际上,在以前的nhibernate版本中,针对长度的检查也用于查询创建.

Actually in previous versions of nhibernate the check against length was implemented also for query creation.

但是,去年SqlDriver的当前实现发生了变化, 有关详细信息,请参见 https://nhibernate.jira.com/browse/NH-3036 解决方法.

But, the current implementation of the SqlDriver got a change last year, see https://nhibernate.jira.com/browse/NH-3036 for details of the fix.

修正:

    // Used from SqlServerCeDriver as well
    public static void SetVariableLengthParameterSize(IDbDataParameter dbParam, SqlType sqlType)
    {
        SetDefaultParameterSize(dbParam, sqlType);

        // no longer override the defaults using data from SqlType, since LIKE expressions needs larger columns
        // https://nhibernate.jira.com/browse/NH-3036
        //if (sqlType.LengthDefined && !IsText(dbParam, sqlType) && !IsBlob(dbParam, sqlType))
        //{
        //  dbParam.Size = sqlType.Length;
        //}

        if (sqlType.PrecisionDefined)
        {
            dbParam.Precision = sqlType.Precision;
            dbParam.Scale = sqlType.Scale;
        }
    }

默认值是您看到的(4000/8000),具体取决于数据类型...

The defaults are what you see (4000/8000) dependent on the data type...

这篇关于NHibernate忽略长度属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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