繁体   English   中英

如何从EF6中的表中读取浮点数据

[英]How to read floating point data from a table in EF6

我尝试从数据库读取数据

  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;

我仍然有例外

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

我修好了它。 我转到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修复。

我希望它也能帮助别人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM