LINQ query on byte? simple property fails on MSSQL 2005 (tinyint)
Description
Using the NH 3.1 LINQ Provider (also 3.2.0CR1), i get failing errors when including a nullable byte property in a query.
[Test] public void LinqQueryOnNullableByteShouldWork() { using (ISession session = this.OpenSession()) { // DomainClass entity = session.Get<DomainClass>(1);
// try LINQ join on byte property var entityQueryable = from x in session.Query<DomainClass>() where x.NullableByteProperty == Convert.ToByte(16) select x;
Assert.IsTrue(entityQueryable.Count() == 1); } }
The generated SQL seems right, but the exception thrown is: System.InvalidCastException : Specified cast is not valid.
When i try ICritera, it works (i'm using mostly LINQ provider).
I am using MS SQL 2005 (sqlexpress and regular server). tinyint is the backing datatype. .NET Framework 4.0 , Visual Studio 2010.
Fixed in 3.3.x eda55d7d0aec86b90b9d38c4ecec1384832cfe8c
Raul Nohea Goodness
July 12, 2011 at 6:54 PM
Thanks, that's too bad if its in the MS LINQ code.
Looks like i will be able to do a workaround-- use int? on my POCO class property, and keep the tinyint datatype on the MS SQL table schema.
Patrick Earl
July 12, 2011 at 4:51 AM
Thanks for the good test. The basic problem is that the MS Linq expression maker turns this: x.NullableByteProeprty == Convert.ToByte(16) into something like this: (int?)x.NullableByteProeprty == (int?)(int)Convert.ToByte(16)
I don't know why it does this, nor do I have a great solution up my sleeve at the moment.
Raul Nohea Goodness
July 11, 2011 at 7:15 PM
Contains failing test for byte? (also passing test w/int?), per the recommended NH/nunit project.
Using the NH 3.1 LINQ Provider (also 3.2.0CR1), i get failing errors when including a nullable byte property in a query.
[Test]
public void LinqQueryOnNullableByteShouldWork()
{
using (ISession session = this.OpenSession())
{
// DomainClass entity = session.Get<DomainClass>(1);
// try LINQ join on byte property
var entityQueryable = from x in session.Query<DomainClass>()
where x.NullableByteProperty == Convert.ToByte(16)
select x;
Assert.IsTrue(entityQueryable.Count() == 1);
}
}
The generated SQL seems right, but the exception thrown is:
System.InvalidCastException : Specified cast is not valid.
When i try ICritera, it works (i'm using mostly LINQ provider).
I am using MS SQL 2005 (sqlexpress and regular server). tinyint is the backing datatype.
.NET Framework 4.0 , Visual Studio 2010.
I will submit a failing test.
HBM mappings (originally i use Fluent NHibernate)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH1234"
default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class name="DomainClass">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="id" />
<generator class="assigned" />
</id>
<property name="NullableByteProperty" type="System.Nullable`1[[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="nullable_byte_property" />
</property>
<property name="NullableIntProperty" type="System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="nullable_Int_property" />
</property>
</class>
</hibernate-mapping>