NHibernate
  1. NHibernate
  2. NH-2789

LINQ query on byte? simple property fails on MSSQL 2005 (tinyint)

    Details

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

      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.

      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>

        Issue Links

          Activity

          Hide
          Raul Nohea Goodness added a comment -

          Contains failing test for byte? (also passing test w/int?), per the recommended NH/nunit project.

          NH2789/
          DomainClass.cs
          Mappings.hbm.xml
          SampleTest.cs

          Show
          Raul Nohea Goodness added a comment - Contains failing test for byte? (also passing test w/int?), per the recommended NH/nunit project. NH2789/ DomainClass.cs Mappings.hbm.xml SampleTest.cs
          Hide
          Patrick Earl added a comment -

          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.

          Show
          Patrick Earl added a comment - 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.
          Hide
          Raul Nohea Goodness added a comment -

          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.

          Show
          Raul Nohea Goodness added a comment - 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.
          Hide
          Alexander I. Zaytsev added a comment -

          Fixed in 3.3.x eda55d7d0aec86b90b9d38c4ecec1384832cfe8c

          Show
          Alexander I. Zaytsev added a comment - Fixed in 3.3.x eda55d7d0aec86b90b9d38c4ecec1384832cfe8c
          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:
              Raul Nohea Goodness
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Who's Looking?