1
votes

The fallowing was working with NHibernate 2.1. Now with NHibernate 3 it doesn't. I have a Table with Column Kind, which has the fallowing Mapping:

Now, when I try do with Linq

.Where(x => x.Kind == (byte)criteria.Value)

where criteria.Value is a byte-Value, I get a InvalidCastException:(could not execute query [ select ... from ProjectType projecttyp0_ where projecttyp0_.Kind=? order by projecttyp0_.Name asc ] Name:p1 - Value:0 ) --->

bei NHibernate.Type.ByteType.Set(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\nhibernate\src\NHibernate\Type\ByteType.cs:Zeile 44. bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in d:\CSharp\NH\nhibernate\src\NHibernate\Type\NullableType.cs:Zeile 180. bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) in d:\CSharp\NH\nhibernate\src\NHibernate\Type\NullableType.cs:Zeile 139. bei NHibernate.Engine.QueryParameters.BindParameters(IDbCommand command, Int32 start, ISessionImplementor session) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\QueryParameters.cs:Zeile 638.

Does I have to change something?

Thanks for your help.

Best Regards, Thomas

3

3 Answers

2
votes

I had almost the same problem: System.InvalidCastException : Specified cast is not valid.

My POCO property was type: byte? My MS SQL 2005 column was type: tinyint NULL

Every time i queried using LINQ to NHibernate on the property, System.InvalidCastException was thrown. Criteria API worked OK.

I ended up filing a bug w/NH project

https://nhibernate.jira.com/browse/NH-2789

Apparently, there is a bug in the MS LINQ code, casting byte to int.

My workaround was to use the type (int?) on my POCO property, but keep TINYINT as the database type.

In your example, criteria.Value would be an int type instead of byte. This will avoid the MS LINQ bug.

0
votes

What type is criteria.Value?

Try replacing (byte)criteria.Value with Convert.ToByte(criteria.Value)

0
votes

The bug filed by @raulg was resolved with version 3.3.x. I was having this problem today with version 3.2, and confirmed that upgrading to 3.3 resolved this issue.