问题
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 file:
<property name="EmailAddress" column="EMAIL_ADDRESS" type="AnsiString" length="120" />
database field:
[EMAIL_ADDRESS] [varchar](120) NULL,
TIA
回答1:
Actually in previous versions of nhibernate the check against length was implemented also for query creation.
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.
Bugfix:
// 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;
}
}
The defaults are what you see (4000/8000) dependent on the data type...
来源:https://stackoverflow.com/questions/20111457/nhibernate-ignores-length-attribute