Oracle数据类型" Specified Cast无效"在C#实体框架中

时间:2017-05-19 06:25:10

标签: c# oracle entity-framework oracle-manageddataaccess

我试图通过在C#Entity Framework中使用Oracle.ManagedDataAccess从oracle数据库获取数据。但是,当数据类型是oracle中的数字时,值为 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**

我试图添加精度,但它没有用。有谁可以帮助我?

2 个答案:

答案 0 :(得分:1)

十进制类型的范围小于您尝试将其设置为的数字,因此出现错误。 Decimal类型值是它的精度,MS Reference文章声明它更适合用于财务计算。

https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/decimal

对于你的示例数据,我倾向于使用double,它的范围很大但不如十进制类型精确

https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/double

答案 1 :(得分:0)

@ rosselder83回答,

我们可以使用Round 例如:

ROUND ( (3600 / MAX (OPTIME) * 7.5), 8) AS TARGETPERDAY,

降低价值。