简体   繁体   English

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

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

I try to read data from database 我尝试从数据库读取数据

  db.Table1.Load(); 

and get exception 并获得例外

Specified cast is not valid.

StackTrace: 堆栈跟踪:

   в 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)

other tables are loaded normally. 其他表正常加载。

like 喜欢

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

Table1 has columns with the type double but the standard delimiter character in my culture is a comma. Table1列的类型为double但是在我的文化中,标准分隔符是逗号。

I thought that this should help but does not work either 我认为这应该有所帮助,但也不起作用

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

I still getting the exception 我仍然有例外

or am I looking for the error is not where I need it? 还是我在寻找错误不在我需要的地方?

I fixed it. 我修好了它。 I go to github and saw that VerifyType method is independent of current culture: 我转到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;

I checked the type of columns in database (it wasn't created by me) and it was text . 我检查了数据库中的列类型(不是我创建的),它是text I fixed by setting types to numeric . 我通过将类型设置为numeric修复。

I hope it help someone too. 我希望它也能帮助别人。

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

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