2

I have a nullable float column in a sql table which is mapped in Nhibernate. The property for this table class is defined:

public virtual float? floatColumn { get; set; }

I have another class which has

public float? anotherFloatPrprty { get; set; }

I get this error

The type System.Double can not be assigned to a property of type System.Nullable`1[System.Single] setter of MyProject

with the following session query

var j = session.CreateSQLQuery("SELECT floatColumn As anotherFloatPrprty from myTable")

So I changed the table to have that column as not null, But I still get the following error.

{"Object of type 'System.Double' cannot be converted to type 'System.Single'."}

Obviously when I don't have any value in floatColumn, I don't get any exceptions, but as soon as some values are inserted to the table (ranging from 0.01 to 1), I get these exceptions.

How do I map nullable float correctly in Nhibernate

EagleFox
  • 1,367
  • 10
  • 34
  • 58

1 Answers1

1

In this case, the SQL keyword float and C# keyword float are confusing. Because they do not match.

As stated in documentation: SqlDbType Enumeration

Member name    Description
...            ...
Float          Double. A floating point number within 
                 the range of -1.79E +308 through 1.79E +308.

Also check:

Summary: use the double

public virtual double? floatColumn { get; set; }
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Thank you Radim. I've read a lot of documents on this issue. Seems like I have no other option than changing the sql column to double. – EagleFox Feb 08 '14 at 15:36
  • Do you mean changing the C# mapping? to double? That makes sense to me. Really. I would say this should not be an iseue, right? Anyhow. Good luck with NHibenrate. Great tool ;) – Radim Köhler Feb 08 '14 at 15:42
  • Yes Radim. I changed the mapping to double, and it worked. Although I am not sure if I should change the sql table to double. At the moment I haven't changed it – EagleFox Feb 09 '14 at 00:26
  • Good to see, that it is working. The precision on the SQL server side is mostly dependent on your Business needs, rights. If this column contains percentage... float(24) would be ok. Also, the *double* takes twice as much space as *single*. But these are "tunings" which you still can adjust later. Now, the connectivity via NHibernate is working... that's great ;) – Radim Köhler Feb 09 '14 at 04:44