簡體   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