How does one make NHibernate stop using nvarchar(4000) for insert parameter strings?

后端 未结 4 2029
梦谈多话
梦谈多话 2021-02-14 09:38

I need to optimize a query that is being produced by a save (insert query) on a domain entity. I\'ve configured NHibernate using Fluent NHibernate.

Here\'s the query ge

4条回答
  •  后悔当初
    2021-02-14 10:13

    Okay this is what we have to do, the SQLClientDriver ignores the length property of the SqlType. So we created a our own driverclass inheriting from SQLClientDriver and override the method GenerateCommand...Something like this:

    public override IDbCommand GenerateCommand(CommandType type, NHibernate.SqlCommand.SqlString sqlString, SqlType[] parameterTypes)
    {
        var dbCommand = base.GenerateCommand(type, sqlString, parameterTypes);
        SetParameterSizes(dbCommand.Parameters, parameterTypes);
        return dbCommand;
    }
    
    private static void SetParameterSizes(IDataParameterCollection parameters, SqlType[] parameterTypes)
    {
        for (int index = 0; index < parameters.Count; ++index)
            SetVariableLengthParameterSize((IDbDataParameter)parameters[index], parameterTypes[index]);
    }
    
    private static void SetVariableLengthParameterSize(IDbDataParameter dbParam, SqlType sqlType)
    {
        SetDefaultParameterSize(dbParam, sqlType);
        if (sqlType.LengthDefined && !IsText(dbParam, sqlType) && !IsBlob(dbParam, sqlType))
            dbParam.Size = sqlType.Length;
        if (!sqlType.PrecisionDefined)
            return;
        dbParam.Precision = sqlType.Precision;
        dbParam.Scale = sqlType.Scale;
    }
    

提交回复
热议问题