任何想法这个代码有什么问题.
这是异常信息:
System.InvalidCastException: Unable to cast object of type ‘System.Int32’ to type ‘System.String’.
at System.Data.sqlClient.sqlBuffer.get_String()
at Read_CPT_Setting(ObjectMaterializer`1 )
at System.Data.Linq.sqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
at 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,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包含以下消息中提到的所有settingID的所有行.
结果:
场景1:
异常对象 – 没有为SettingID = 142176检索数据 – 内部上下文 – 使用SettingID = 142176查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 – – DBSetting什么都不是
不寻常的对象 – MRDD解决方案 – 没有检索到SETID = 142176的数据输入 – 内部上下文 – 使用SettingID = 142176查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 – – DBSetting什么都不是
场景2
SettingID = 138145– ENTER – 内部上下文中的异常异常 – 使用SettingID = 138145查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 – – 指定的强制转换无效.
不寻常的对象 – MRDD解决方案 – 没有检索到SETID = 138145的数据 – 内部上下文 – 使用SettingID = 138145查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 –
场景3
SettingID = 164638– ENTER – 内部上下文中的异常异常 – 使用SettingID = 164638查询 – 在SingleOrDefault之前 – –Index超出数组范围.
不寻常的对象 – MRDD解决方案 – 没有检索到SETID = 164638的数据输入 – 内部上下文 – 使用SettingID = 164638查询 – 在SingleOrDefault之前 –