NHibernate
  1. NHibernate
  2. NH-2812

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

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 3.1.0
    • Fix Version/s: 3.3.1.GA
    • Component/s: Linq Provider
    • Labels:

      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(); }

      }

        Issue Links

          Activity

          Hide
          CBP added a comment -

          I can confirm this bug occurs with SQL Server.

          Switching to QueryOver works.

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

          Show
          CBP added a comment - I can confirm this bug occurs with SQL Server. Switching to QueryOver works. I cannot find any workaround to perform at the mapping level.
          Hide
          Alexander I. Zaytsev added a comment -

          Fixed in 3.3.x

          Show
          Alexander I. Zaytsev added a comment - Fixed in 3.3.x
          Hide
          Alexander I. Zaytsev added a comment -

          Closed after NH 3.3.1.GA release

          Show
          Alexander I. Zaytsev added a comment - Closed after NH 3.3.1.GA release

            People

            • Assignee:
              Alexander I. Zaytsev
              Reporter:
              Scott Whitlock
            • Votes:
              3 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Who's Looking?