在c#中使用sql float的Nhibernate float问题

时间:2014-02-08 03:48:41

标签: c# sql-server-2008 nhibernate floating-point

我在sql表中有一个可以为空的浮点列,它在Nhibernate中映射。定义了此表类的属性:

public virtual float? floatColumn { get; set; }

我有另一个有

的课程
public float? anotherFloatPrprty { get; set; }

我收到此错误

  

无法将System.Double类型分配给MyProject的System.Nullable`1 [System.Single] setter类型的属性

使用以下会话查询

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

所以我更改了表,使该列不为null,但我仍然收到以下错误。

  

{"类型对象' System.Double'无法转换为类型' System.Single'。"}

显然,当我在floatColumn中没有任何值时,我没有得到任何异常,但只要一些值插入到表中(范围从0.01到1),我就会得到这些异常

如何在Nhibernate中正确映射可空浮点

1 个答案:

答案 0 :(得分:1)

在这种情况下, SQL 关键字 float C# 关键字 {{1}令人困惑。因为它们不匹配。

如文档中所述:SqlDbType Enumeration

float

同时检查:

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

double