I get the same error with the same version of NHibernate using the Linq2Nhibernate query, for example:
var details = (from d in repository.AllEntities where (d.OtherTable.Field == someCriteria && d.LineIndex == 1) select d).ToList();
In this case, LineIndex displayed as a .NET Byte (cannot be NULL). The top of my stack trace is as follows:
NHibernate.Type.ByteType.Set(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\ByteType.cs: line 44 NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs: line 180 NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs: line 139 NHibernate.Engine.QueryParameters.BindParameters(IDbCommand command, Int32 start, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\QueryParameters.cs: line 630 ...
Looking at the source, the Set method looks like this:
public override void Set(IDbCommand cmd, object value, int index) { ((IDataParameter) cmd.Parameters[index]).Value = (byte) value; }
Therefore, it tries to pass the object to Byte and get an invalid listing. Interestingly, when I passed my literal 1 to Byte , I still get the same error. This also happens if I use const byte . So I think this is something higher than the stack that takes Byte and does something weird with it.
It is particularly interesting that it makes a NullSafeSet when the field is not NULL. As an experiment, I changed my LineIndex property to int , and I did not get an invalid exception. This seems like a bug in NHibernate. I will try to stick to this in the NHibernate tracker block.
Edit
Please note that this similar error NH-2485 was closed as not a problem.
Another similar / overlapping error:
- NH-2789 LINQ query by byte? simple property does not work on MSSQL 2005 (tinyint)
I posted this error:
- NH-2812 Running a Linq query for a non-null property raises an InvalidCastException
Edit
Version 3.3.1 and higher solves this problem.