【问题标题】:Strange Behavior (Bug) of Oracle Data Provider (InvalidCastException: Specified Cast is not Valid)Oracle 数据提供者的奇怪行为 (Bug) (InvalidCastException: Specified Cast is not Valid)
【发布时间】:2017-06-21 07:20:26
【问题描述】:

Oracle ManagedDataAccess 似乎有问题,如果您尝试执行以下示例,您可能会遇到错误(这是意外的;更改为其他字段但相同的数据类型可能不会导致问题);尽管实体框架已经毫无问题地实现了查询:

INVENTORY
.GroupBy(inv => 1)
.Select(invGroup => new 
{
    GrossWeight = invGroup.Sum(inv => inv.GROSS_WEIGHT / inv.BASE_QTY_PER_UNIT)
})

前面的代码会导致如下异常:

   InvalidCastException
   Specified cast is not valid. 
   StackTrace
   at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
   at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
   at lambda_method(Closure , Shaper )
   at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   at UserQuery

在网上搜索,似乎其他人也有这个例外 https://chrismay.org/2015/07/30/bug-in-oracledatareader-causing-invalidcastexception/

【问题讨论】:

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


    【解决方案1】:

    有两种解决方法:

    选项#1 将所需的值转换为浮点数

    INVENTORY
    .GroupBy(inv => 1)
    .Select(invGroup => new 
    {
        GrossWeight = invGroup.Sum(inv => (float)inv.GROSS_WEIGHT / (float)inv.BASE_QTY_PER_UNIT)
    })
    

    选项#2 通过使用 AsEnumerable() 在内存中执行求和,如下所示

    INVENTORY
    .GroupBy(inv => inv.BASE_QTY_PER_UNIT)
    .Select(invGroup => new 
    {
        GrossWeight = invGroup.Sum(inv => inv.GROSS_WEIGHT),
        BaseQuantityPerUnit = invGroup.Key
    })
    .AsEnumerable()
    .Select(anony => new
    {
        GrossWeight = anony.GrossWeight / anony.BaseQuantityPerUnit
    })
    

    【讨论】:

      猜你喜欢
      • 2020-05-26
      • 1970-01-01
      • 2012-09-15
      • 2023-02-15
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多