Fixed
Details
Details
Assignee
Alex Zaytsev
Alex ZaytsevReporter
Scott Whitlock
Scott WhitlockLabels
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
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();
}
}