[英]Oracle data type “Specified Cast is not valid” in C# Entity Framework
我正在嘗試通過在C#實體框架中使用Oracle.ManagedDataAccess從Oracle數據庫中獲取數據。 但是,當數據類型為oracle中的number且值為6.17880949622285E-11時,出現錯誤。 這是錯誤消息
{
"ClassName": "System.InvalidCastException",
"Message": "Specified cast is not valid.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)\r\n at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)\r\n at lambda_method(Closure , Shaper )\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)\r\n at lambda_method(Closure , Shaper )\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()\r\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at WebApiService.Master.Facul.FaculService.FetchAllProportionalByParameters(FaculModel faculSearch) in D:\\Projects\\Reins\\WebApiService\\Master\\Facul\\FaculService.cs:line 176\r\n at PKBL.Controllers.Master.FaculController.FetchAllProportionalByParameters(FaculModel faculSearch) in D:\\Projects\\Reins\\REINS\\Controllers\\Master\\FaculController.cs:line 74",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": "8\nGetDecimal\nOracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342\nOracle.ManagedDataAccess.Client.OracleDataReader\nSystem.Decimal GetDecimal(Int32)",
"HResult": -2147467262,
"Source": "Oracle.ManagedDataAccess",
"WatsonBuckets": null
}
我已經對其進行了調查,發現問題在於數據類型。 順便說一句,我首先使用代碼,這是我的EF課
public class MasterFacul : BaseEntityModel
{
....
public decimal? FacWrtShr { get; set; } <- **The Problem**
我試圖增加精度,但是沒有用。 誰能幫我?
十進制類型的范圍小於您嘗試設置的范圍,因此會出現錯誤。 《十進制類型》值是它的精度,《 MS參考》文章指出,“十進制類型”值可更好地用於財務計算。
https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/language-reference/keywords/decimal
對於您的示例數據,我傾向於使用雙精度型,其范圍很大,但不如十進制類型精確
https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/language-reference/keywords/double
正如@ rosselder83回答的那樣,
我們可以使用Round ex:
ROUND ( (3600 / MAX (OPTIME) * 7.5), 8) AS TARGETPERDAY,
降低價值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.