【问题标题】:How to read floating point data from a table in EF6如何从 EF6 中的表中读取浮点数据
【发布时间】:2018-06-03 21:23:44
【问题描述】:

我尝试从数据库中读取数据

  db.Table1.Load(); 

并获得异常

Specified cast is not valid.

堆栈跟踪:

   в System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
   в System.Data.SQLite.SQLiteDataReader.GetDouble(Int32 i)
   в System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.ReadDoubl
e(DbDataReader reader, Int32 ordinal)
   в System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initializ
e(DbDataReader reader, DbSpatialDataReader spatialDataReader, Type[] columnTypes
, Boolean[] nullableColumns)
   в System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initializ
e(String providerManifestToken, DbProviderServices providerServices, DbDataReade
r reader, Type[] columnTypes, Boolean[] nullableColumns)
   в System.Data.Entity.Core.Objects.Internal.BufferedDataReader.Initialize(Stri
ng providerManifestToken, DbProviderServices providerServices, Type[] columnType
s, Boolean[] nullableColumns)
   в System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[T
ResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   в System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResult
s>b__a()
   в System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`
1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, B
oolean releaseConnectionOnSuccess)
   в System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResult
s>b__9()
   в System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult]
(Func`1 operation)
   в System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMerg
eOption)
   в System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.I
Enumerable<T>.GetEnumerator>b__0()
   в System.Lazy`1.CreateValue()
   в System.Lazy`1.LazyInitValue()
   в System.Lazy`1.get_Value()
   в System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   в System.Data.Entity.QueryableExtensions.Load(IQueryable source)

其他表加载正常。

喜欢

 db.Table2.Load(); // works just fine, all columns has type string

Table1 的列类型为double,但我的文化中的标准分隔符是逗号。

我认为这应该会有所帮助,但也不起作用

            System.Threading.Thread.CurrentThread.CurrentCulture =
                System.Globalization.CultureInfo.InvariantCulture;
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                System.Globalization.CultureInfo.InvariantCulture;

我仍然遇到异常

还是我在寻找错误不在我需要的地方?

【问题讨论】:

  • 你为什么要对我投反对票?
  • 任何时候你说你“得到例外”,你必须包括entire expection,而不仅仅是你挑选的一两个词,包括the code that throws the exception。此外,“不起作用”什么也没告诉我们。它有什么作用?你期望它做什么?
  • @DourHighArch 感谢您的反馈。这是一个完整的异常消息,如果可以的话,我会提供更多。我写了引发异常的行。不起作用意味着我仍然得到异常

标签: c# wpf entity-framework entity-framework-6 double


【解决方案1】:

我修好了。我去github看到VerifyType方法是独立于当前文化的:

case TypeAffinity.Double:
          if (typ == DbType.Single) return affinity;
          if (typ == DbType.Double) return affinity;
          if (typ == DbType.Decimal) return affinity;
          if (typ == DbType.DateTime) return affinity;
break;

我检查了数据库中的列类型(不是我创建的),它是text。我通过将类型设置为numeric 进行了修复。

我希望它也能帮助别人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多