在nHibernate中调试"字符串或二进制数据将被截断"错误

Gab*_*art 2 c# database nhibernate validation

我在我的应用程序中调试间歇性截断错误.我的日志中的错误如下所示:

System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated. Generated: Tue, 02 Nov 2010 03:55:18 GMT

NHibernate.Exceptions.GenericADOException: could not insert:
[DataModel.Product][SQL: INSERT INTO [Product] (Fields) VALUES (?, ?,...);
select SCOPE_IDENTITY()] ---> 
System.Data.SqlClient.SqlException: String or binary data would be truncated. 
The statement has been terminated.    
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
Run Code Online (Sandbox Code Playgroud)

请注意,错误不会为抛出错误的sql提供参数值,只有占位符:(?, ?,...).有没有办法从nHibernate获取这些?像这样的东西:

try {
    ...
    Session.Flush(); // throws
}
catch (GenericADOException ex) {

    // want to get the bad parameter values, 
    // so I can re-throw a more helpful exception
}
Run Code Online (Sandbox Code Playgroud)

小智 6

大多数情况下,当string or binary data truncated....来自Nhibernate数据的字符数多于字段长度时,这来自Ado异常.例如,如果表db字段是nvarchar(50),但正在努力插入超过50个字符,则会发生此异常.解决方案是增加表Db字段长度或减少插入的数据字符.