System.Data.SQLite在string列中键入affinity int

时间:2011-10-20 05:22:52

标签: vb.net asp.net-mvc-3 entity-framework-4.1 system.data.sqlite

从我读到的关于SQLite的内容来看,它有一个名为Type Affinity的功能,这意味着数据类型并没有严格执行(More info here)。

我正在使用带有EF4.1的POCO对象从另一个应用程序中创建的数据库中读取,当所谓的字符串字段中包含整数时会出现问题。如果int字段中包含非数字字符串,我可以理解抛出异常...但为什么这会搞砸我的应用程序?

这是我的测试代码的一个示例,我删除了其他所有内容,直到剩下最少的代码才能弹出。

Public Class TVSerie
    Public Property Id As Integer
    Public Property PrettyName As String
End Class

Public Class TVSeriesDb
    Inherits DbContext
    Public Property TVSeries As DbSet(Of TVSerie)
End Class

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Function Index() As ActionResult
        Dim db As New TVSeriesDb
        Dim sb As New StringBuilder
        Dim series = db.TVSeries.Where(Function(c) c.PrettyName.Length > 0)
        For Each s In series
            sb.Append(s.Id & "-" & s.PrettyName & "<br/>")
        Next
        Return Content(sb.ToString)
    End Function
End Class

错误在TVSeries 24的“PrettyName”列中弹出:

[InvalidCastException: Specified cast is not valid.]
System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ) +492
System.Data.SQLite.SQLiteDataReader.GetString(Int32 i) +131
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target,   Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +0
   System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +72
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +251
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +28
   System.Data.Common.Internal.Materialization.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) +342
   System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling(Int32 ordinal, String propertyName, String typeName) +79
   lambda_method(Closure , Shaper ) +167
   System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) +218
   lambda_method(Closure , Shaper ) +291
   System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +170
   System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +84
   Test.Controllers.HomeController.Index() in C:\Projects\PlayListShuffler\Test\Controllers\HomeController.vb:14
   lambda_method(Closure , ControllerBase , Object[] ) +96
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

1 个答案:

答案 0 :(得分:0)

虽然sqlite不是强类型的(它与Python这样的动态类型语言非常相似),但是在包装器中强制执行.NET的强类型性质。因此,在读取表时检测到的列类型是Reader.Getxxx(n)所需的类型。