FluentNHibernate映射; 无法使用比例/精度映射双精度或小数

bre*_*dog 9 .net database nhibernate nhibernate-mapping fluent-nhibernate

我第一次使用FluentNHibernate,尝试将类映射到SQL Express数据库.通常它可以工作,但我无法将Double或Decimal属性类型映射到特定的比例/精度.下面显示了我使用SchemaUpdate.Execute反复测试的单个属性的结果.在任何情况下我都无法让它发挥作用.

听到一些不符合我预期的映射的解释(2-8)真的很有帮助吗?

// Ok mappings:
Run Code Online (Sandbox Code Playgroud)

1)十进制:映射(函数(x)x.Balance)>>十进制(19,5 )

// Mappings "errors":
Run Code Online (Sandbox Code Playgroud)

2)Double:Map(Function(x)x.Balance).CustomSqlType("decimal") >> Decimal(18,0) - 为什么0 precision是这里的默认映射?

3)Double:Map(Function(x)x.Balance)>> Float,但是; 在运行SchemaValidator之后: HibernateException:FnhDb.dbo.Account中的列类型错误,用于列Balance.发现:浮动,预期双精度

4)十进制:映射(函数(x)x.Balance).Scale(9).精度(2) >> SqlException:列'Balance'的标度(9)必须在0到2的范围内.

5,6)十进制或双数:映射(函数(x)x.Balance).Scale(9).精度(2).CustomSqlType("numeric") >> numeric(18,0)

7,8)十进制或双精度:映射(函数(x)x.Balance).Scale(9).精度(2).CustomSqlType("decimal") >>十进制(18,0)


编辑: 我在这里包含代码和案例(4)的hbm.xml(导出):

Public Class AccountMap
    Inherits ClassMap(Of Account)

    Public Sub New()
        MyBase.New()

        Id(Function(x) x.Id).GeneratedBy.Identity()
        Map(Function(x) x.Balance).Scale(9).Precision(2)   
        Map(Function(x) x.Deposits)
        Map(Function(x) x.WithDrawals)
    End Sub
End Class

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="false">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="RoboTrader.Account, RoboTrader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Account`">
    <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <property name="Balance" type="System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Balance" precision="2" scale="9" />
    </property>
    <property name="Deposits" type="System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Deposits" />
    </property>
    <property name="WithDrawals" type="System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="WithDrawals" />
    </property>
  </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

EDIT2:

顺便说一句,这不是一个VB问题.我在C#项目中遇到了完全相同的问题.可能是MsSql2008配置Sql Express 2008 R2不兼容吗?


EDIT3:

Option Strict On
Run Code Online (Sandbox Code Playgroud)

Imports System.Collections.Generic Imports System.Text Imports System

Public Class Account
    Public Sub New()
        MyBase.New()
End Sub

Private _Id As Integer

Private _Balance As Double

Private _Deposits As Integer

Private _WithDrawals As Integer


Public Overridable Property Id() As Integer
    Get
        Return _Id
    End Get
    Set(ByVal value As Integer)
        _Id = value
    End Set
End Property
Public Overridable Property Balance() As Double
    Get
        Return _Balance
    End Get
    Set(ByVal value As Double)
        _Balance = value
    End Set
End Property
Public Overridable Property Deposits() As Integer
    Get
        Return _Deposits
    End Get
    Set(ByVal value As Integer)
        _Deposits = value
    End Set
End Property
Public Overridable Property WithDrawals() As Integer
    Get
        Return _WithDrawals
    End Get
    Set(ByVal value As Integer)
        _WithDrawals = value
    End Set
End Property




End Class
Run Code Online (Sandbox Code Playgroud)

Yog*_*esh 12

首先,你的理解PrecisionScale错误.Precision永远高于Scale.有关更好的理解,请参阅此MSDN文档,其中说明:

精度是数字中的位数.Scale是数字中小数点右边的位数.例如,数字123.45的精度为5,标度为2.

在你的第二个例子中,即Decimal(18,0)0 Scale,不是Precision.Precision是18岁.

其次,您的映射应该是这样的:

Map(Function(x) x.Balance).CustomSqlType("decimal").Precision(9).Scale(2);
Run Code Online (Sandbox Code Playgroud)

如果设置了CustomSqlType("decimal")设置后Precision,并Scale通过你所做的设置都将重置.

编辑:
double在宣言中使用,我认为您应该使用decimal.看到这个问题,知道原因.double是一个浮动类型变量,所以它float默认映射到a ,直到你提到,或者直到Precision它高于7.如果你更改Balanceto 的声明decimal,你可以像这样映射属性没有任何问题:

Map(Function(x) x.Balance).Precision(9).Scale(2)
Run Code Online (Sandbox Code Playgroud)