Performing a Linq query on a non-null byte property throws an InvalidCastException

Description

Background: StackOverflow Question (http://stackoverflow.com/questions/6521285/query-a-byte-property-with-nhibernate-cause-invalid-cast-error/6860216#6860216)

I'm using SQLLite with FluentNHibernate and NHibernate version of 3.1.0.4000. When I try to use Linq to query by a byte property, I get an InvalidCastException. I've shrinked it down into a test project that exercises the bug with a unit test in MSTest (see attached). The entity looks like this:

public class MyEntity
{
public virtual Guid Id { get; private set; }
public virtual int MyIntProperty { get; set; }
public virtual byte MyByteProperty { get; set; }
}

Mapping:

public class MyEntityMap : ClassMap<MyEntity>
{
public MyEntityMap()
{
Table("MYENTITY");
Id(x => x.Id)
.Column("MYENTITY_ID")
.GeneratedBy.Guid();
Map(x => x.MyByteProperty)
.Column("MY_BYTE_PROPERTY")
.Not.Nullable();
Map(x => x.MyIntProperty)
.Column("MY_INT_PROPERTY")
.Not.Nullable();
}
}

Just for reference, this unit test on the Int property passes:

[TestMethod]
public void Should_be_able_to_query_by_int_type()
{
using (var session = sessionFactory.OpenSession(masterConnection))
{
(from e in session.Query<MyEntity>()
where e.MyIntProperty == 1 // PASSES
select e)
.ToList();
}
}

...but this one, substituting the int property for the byte property fails and throws an InvalidCastException:

[TestMethod]
public void Should_be_able_to_query_by_byte_type()
{
using (var session = sessionFactory.OpenSession(masterConnection))
{
(from e in session.Query<MyEntity>()
where e.MyByteProperty == 1 // FAILS
select e)
.ToList();
}
}

Environment

None

Attachments

1
  • 28 Jul 2011, 03:12 PM

Activity

Show:

Alex Zaytsev August 2, 2012 at 8:54 AM

Closed after NH 3.3.1.GA release

Alex Zaytsev May 23, 2012 at 7:55 PM

Fixed in 3.3.x

CBP November 7, 2011 at 4:02 AM

I can confirm this bug occurs with SQL Server.

Switching to QueryOver works.

I cannot find any workaround to perform at the mapping level.

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created July 28, 2011 at 3:12 PM
Updated August 2, 2012 at 8:54 AM
Resolved May 23, 2012 at 7:55 PM
Who's Looking?