C# Entity Framework 中的 Oracle 数据类型 "Specified Cast is not valid"
Oracle data type "Specified Cast is not valid" in C# Entity Framework
我正在尝试使用 C# Entity Framework 中的 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 class
public class MasterFacul : BaseEntityModel
{
....
public decimal? FacWrtShr { get; set; } <- **The Problem**
我试过提高精度,但没用。谁能帮帮我?
小数类型的范围小于您尝试设置的数字,因此出现错误。 Decimal 类型的值是它的精度,MS Reference 文章指出它更适合用于财务计算。
https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/decimal
对于您的示例数据,我倾向于使用双精度,它的范围很大但不如小数类型精确
https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/double
正如@rosselder83 回答的那样,
我们可以用Round
例如:
ROUND ( (3600 / MAX (OPTIME) * 7.5), 8) AS TARGETPERDAY,
减少值。
我正在尝试使用 C# Entity Framework 中的 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 class
public class MasterFacul : BaseEntityModel
{
....
public decimal? FacWrtShr { get; set; } <- **The Problem**
我试过提高精度,但没用。谁能帮帮我?
小数类型的范围小于您尝试设置的数字,因此出现错误。 Decimal 类型的值是它的精度,MS Reference 文章指出它更适合用于财务计算。
https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/decimal
对于您的示例数据,我倾向于使用双精度,它的范围很大但不如小数类型精确
https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/double
正如@rosselder83 回答的那样,
我们可以用Round 例如:
ROUND ( (3600 / MAX (OPTIME) * 7.5), 8) AS TARGETPERDAY,
减少值。