带有实体框架的Sqlite - 错误:“指定的强制转换无效”

时间:2011-08-10 15:29:15

标签: .net vb.net entity-framework sqlite

字段类型是金钱,如果我在字段中输入'0'或'1',我会收到此错误:

System.Reflection.TargetInvocationException:

Exception has been thrown by the target of an invocation. --->
System.InvalidCastException: Specified cast is not valid.

 at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
 at System.Data.SQLite.SQLiteDataReader.GetBoolean(Int32 i)

 --- End of inner exception stack trace ---

这是来自模特设计师:

<Property Name="Amount" Type="decimal" Precision="53" />  

====

    <EdmScalarPropertyAttribute(EntityKeyProperty:=False, IsNullable:=True)>
<DataMemberAttribute()>
Public Property Amount() As Nullable(Of Global.System.Decimal)
    Get
        Return _Amount
    End Get
    Set(ByVal value As Nullable(Of Global.System.Decimal))
        OnAmountChanging(Value)
        ReportPropertyChanging("Amount")
        _Amount = StructuralObject.SetValidValue(value)
        ReportPropertyChanged("Amount")
        OnAmountChanged()
    End Set
End Property

Private _Amount As Nullable(Of Global.System.Decimal)
Private Partial Sub OnAmountChanging(value As Nullable(Of Global.System.Decimal))
End Sub

Private Partial Sub OnAmountChanged()
End Sub

获取错误的代码:

 Dim Query = From c In EnData.Transactions Where c.TranID = 660 ' this tran is the amount 0

        For Each tran In Query 'Error here

        Next

2 个答案:

答案 0 :(得分:4)

我遇到了问题......它不是金额字段。另一列是位类型,有时它不喜欢我写错误或真实的方式。导致问题的原因。

解决方案是使用1表示true,0表示false表示,总是正常。

答案 1 :(得分:0)

我为Microsoft SQL Server编写SQL。如果以下示例对SqLite无效,请告诉我,我将删除此答案。

问题:什么是:StructuralObject.SetValidValue(value)?这是一个自定义功能吗?如果是这样,你能定义吗?

也许修改VB代码以执行以下帮助:

Public Property Amount() As System.Decimal
    Get
        Return _Amount
    End Get
    Set(ByVal value As Global.System.Decimal)
        OnAmountChanging(Value)
        ReportPropertyChanging("Amount")
        If value = 0 Then
          _Amount = 0.00M
        Else If value = 1 Then
          _Amount = 1.00M
        Else
          _Amount = StructuralObject.SetValidValue(value)
        End If
        ReportPropertyChanged("Amount")
        OnAmountChanged()
    End Set
End Property

通常情况下,我更喜欢将“最真实”的案例置于顶端,但我的VB技能并不是那么好。

我希望这会有所帮助。