Dot-Net

FluentNHibernate 映射;無法使用比例/精度映射雙精度或小數

  • January 31, 2011

我第一次使用FluentNHibernate,試圖將類映射到SQL Express數據庫。一般來說它可以工作,但我無法將Double 或 Decimal屬性類型映射到特定的 scale/precision下面顯示了我使用SchemaUpdate.Execute反複測試的單個屬性的結果。在任何情況下,我都無法讓它工作。

聽到一些對無法按我預期(2-8)工作的映射的解釋真的很有幫助嗎?

// Ok mappings:

1)十進制:映射(函式(x)x.Balance)**>>**十進制(19、5)

// Mappings "errors":

2) Double : Map(Function(x) x.Balance).CustomSqlType(“decimal”) >> Decimal(18,0) - 為什麼這裡的預設映射是 0 精度?

3) 雙精度: Map(Function(x) x.Balance) >> Float , 但是; 在以下執行SchemaValidator時: HibernateException:FnhDb.dbo.Account 中列餘額的列類型錯誤。找到:浮動,預期雙精度

4) 十進制:Map(Function(x) x.Balance).Scale(9).Precision(2) >> SqlException:“餘額”列的比例 (9) 必須在 0 到 2 的範圍內。

5,6) 十進製或雙精度: Map(Function(x) x.Balance).Scale(9).Precision(2).CustomSqlType(“numeric”) >> numeric(18,0)

7,8) 十進製或雙精度: Map(Function(x) x.Balance).Scale(9).Precision(2).CustomSqlType(“decimal”) >> 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>

編輯2:

順便說一句,這不是VB問題。我在C#項目中遇到了完全相同的問題。會不會是與Sql Express 2008 R2不兼容的MsSql2008 配置


編輯3:

Option Strict On

導入 System.Collections.Generic 導入 System.Text 導入系統

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

首先,你對Precisionand的理解Scale是錯誤的。Precision總是高於Scale。請參閱此 MSDN 文件以獲得更好的理解,其中指出:

精度是數字中的位數。比例是數字中小數點右側的位數。例如,數字 123.45 的精度為 5,小數位數為 2。

在您的第二個範例中,即Decimal(18,0)0 是Scale,不是PrecisionPrecision是 18 歲。

其次,您的映射應該是這樣的:

Map(Function(x) x.Balance).CustomSqlType("decimal").Precision(9).Scale(2);

如果您在設置和CustomSqlType("decimal")之後設置,您所做的設置將被重置。Precision``Scale

編輯:

double在聲明中使用,我認為您應該使用decimal. 請參閱此問題以了解原因。double是一個浮動類型變數,因此預設情況下它被映射到 afloat直到您另有說明或直到Precision高於 7。如果您更改Balanceto的聲明decimal,您可以像這樣映射屬性而不會出現任何問題:

Map(Function(x) x.Balance).Precision(9).Scale(2)

引用自:https://stackoverflow.com/questions/4846471