你有什么想法这段代码有什么问题吗?
以下是异常信息:
System.InvalidCastException: 无法将类型为'System.Int32'的对象强制转换为类型'System.String'。 在 System.Data.SqlClient.SqlBuffer.get_String() 在 Read_CPT_Setting(ObjectMaterializer`1 ) 在 System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext() 在 System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) 在 System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source) 在 CPT.Service.SettingLinqProvider.GetSettingFromDBById(Int32 SettingId)
代码:
Public Function GetSettingFromDBById(ByVal SettingId As Integer) As ReturnObject(Of Model.ISettingBase)
Dim retObj As New ReturnObject(Of Model.ISettingBase)
Dim dbSetting As CPT_Setting
Try
Dim _cptDB As New CPT.Data.CPTLinqRepository.DB(_connString)
Using _cptDB
dbSetting = (From s In context.CPT_Settings _
Where s.SettingId = settingId _
Select s).SingleOrDefault
If dbSetting IsNot Nothing Then
retObj.ReturnValue = Mapping.Setting.MapDBToModel(dbSetting)
End If
End Using
Catch ex As Exception
retObj.ReturnValue = Nothing
retObj.AddErrorMessage("Error returning the site: " & ex.Message)
_log.Error(ex.Message, _userId, ex)
End Try
If retObj.ReturnValue Is Nothing Then
retObj.AddErrorMessage("Site Not Found.")
End If
Return retObj
End Function
我已经在上面的代码中加入了一些日志记录,每行代码之后都有。
Public Function GetSettingFromDBById(ByVal SettingId As Integer) As ReturnObject(Of Model.ISettingBase)
Dim retObj As New ReturnObject(Of Model.ISettingBase)
Dim dbSetting As CPT_Setting
Dim SettingsList As New List(Of CPT_Setting)
Dim errStr As String = " ENTER "
Try
Dim _cptDB As New CPT.Data.CPTLinqRepository.DB(_connString)
Using _cptDB
errStr &= " - Inside Context "
If _cptDB Is Nothing Then
errStr &= " - With Context is Nothing "
_log.Error("Unusual Object - Unable to create connection object - Object is NOTHING", _userId)
End If
If System.DBNull.Value.Equals(_cptDB) Then
errStr &= " - With Context is NULL "
_log.Error("Unusual Object - Unable to create connection object - Object is NULL", _userId)
End If
errStr &= " - Querying With SettingID = " & SettingId.ToString()
Dim dbSettingTemp = (From s In context.CPT_Settings _
Where s.SettingId = settingId _
Select s)
If dbSettingTemp Is Nothing Then
errStr &= " -- Nothing is returned from DB - Object is NOTHING -- "
_log.Error(errStr, _userId)
End If
If System.DBNull.Value.Equals(dbSettingTemp) Then
errStr &= " -- Nothing is returned from DB - Object is NULL -- "
_log.Error(errStr, _userId)
End If
errStr &= " -- Before SingleOrDefault -- "
dbSetting = dbSettingTemp.SingleOrDefault
errStr &= " -- After SingleOrDefault -- "
If dbSetting IsNot Nothing Then
If System.DBNull.Value.Equals(dbSetting) Then
errStr &= " - NULL OBJECT RETURNED - Before Mapping "
_log.Error("Unusual Exception - NULL OBJECT RETURNED " & errStr, _userId)
End If
retObj.ReturnValue = Mapping.Setting.MapDBToModel(dbSetting)
errStr &= " - After Mapping With SettingID=" & dbSetting.SettingId.ToString() & " SettingName=" & dbSetting.SettingName.ToString() & " StartDate=" & dbSetting.StartDate.ToShortDateString() & " EndDate=" & dbSetting.EndDate.ToShortDateString()
Else
errStr &= " - DBSetting Is Nothing "
_log.Error("Unusual Object - No Data Retrieved for SettingID=" & SettingId.ToString() & " " & errStr, _userId)
End If
End Using
Catch ex As Exception
retObj.ReturnValue = Nothing
retObj.AddErrorMessage("Error returning the site: " & ex.Message)
_log.Error("Unusual Exception for SettingID=" & SettingId.ToString() & "--" & errStr & "--" & ex.Message, _userId, ex)
End Try
If retObj.ReturnValue Is Nothing Then
retObj.AddErrorMessage("Site Not Found.")
_log.Info("Unusual Object - MRDD Solutions - No Data Retrieved for SettingID=" & SettingId.ToString() & " " & errStr, _userId)
End If
Return retObj
End Function
注意:DB中包含以下消息中提到的所有settingIDs的行。
结果:
场景1:
异常对象-未检索到SettingID = 142176的数据 ENTER-在上下文内-使用SettingID = 142176进行查询-- Before SingleOrDefault -- - After SingleOrDefault -- - DBSetting为null
异常对象-MRDD Solutions-未检索到SettingID = 142176的数据 ENTER-在上下文内-使用SettingID = 142176进行查询-- Before SingleOrDefault -- - After SingleOrDefault -- - DBSetting为null
场景2:
SettingID = 138145的异常情况 ENTER-在上下文内-使用SettingID = 138145进行查询-- Before SingleOrDefault -- - After SingleOrDefault -- --指定的转换无效。
异常对象-MRDD Solutions-未检索到SettingID = 138145的数据 ENTER-在上下文内-使用SettingID = 138145进行查询-- Before SingleOrDefault -- - After SingleOrDefault --
场景3:
SettingID = 164638的异常情况 ENTER-在上下文内-使用SettingID = 164638进行查询-- Before SingleOrDefault -- --索引超出了数组界限。
异常对象-MRDD Solutions-未检索到SettingID = 164638的数据 ENTER-在上下文内-使用SettingID = 164638进行查询-- Before SingleOrDefault --
CPT_Setting
类中的属性和数据库表列之间是否存在类型不匹配的问题?比如:某个列是文本类型,但对应的属性却是整型。 - Gert ArnoldWhere s.SettingId = **SettingId** _
吗?此外,“CPT_Setting”类型可能拥有答案。那个类型上是否有需要可空而没有的属性?在使用LINQ对象将数据转储到没有可空类型的对象中时,我经常遇到这个转换问题。或者可能是返回空字符串的varchar类型被隐式转换为整数属性。 - atconway