【问题标题】:Oracle with EntityFramework cause OverflowException: Value was either too large or too small for an Int16Oracle 与 EntityFramework 导致溢出异常:对于 Int16,值太大或太小
【发布时间】:2016-10-13 21:55:56
【问题描述】:

我有一个简单的 linQ 表达式

var list = db.MyEntity.Where(x=> x.fields == 'stringdata').toList();

在运行时,这个表达式抛出一个异常:

[OverflowException: Value was either too large or too small for an Int16.]
       System.Convert.ToInt16(Int32 value) +6765512
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +14182618
       Oracle.ManagedDataAccess.Client.OracleDataReader.ChangeType(Object sourceValue, Type targetType) +810
       Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i) +4640
       System.Data.Entity.Core.Common.Internal.Materialization.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) +229
       lambda_method(Closure , Shaper ) +3648
       System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) +244
       lambda_method(Closure , Shaper ) +438
       System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +383

我的 EntityFramework 绑定在 Oracle 表上。我怀疑一列的数据大于 Int16。

  • 为什么 EntityFramework 没有设置正确的类型和或 精度?
  • 在哪里以及如何解决这个问题,手动编辑 edmx?
  • 如果我手动修复它,下次我是否会被设计覆盖 更新我的 edmx 架构?

【问题讨论】:

  • 通常这意味着您尝试绑定的数据源和 C# 类具有冲突的数据类型。 IE。您的 C# 类有一个 int,但相应的列不是 int。 ---- 有没有试过 FineOne() 或者 First(),看看是不是随机数据,每一行都错了?
  • @mmcrae 不错!我会试试的。现在......我正在考虑返回一个匿名类型 new { field, field, field....} 也许我可以隔离导致错误的字段

标签: c# oracle entity-framework


【解决方案1】:

我找到了。我得到了在我的 Oracle 数据库中定义为 Number(5) 的列。似乎 EF 映射 number(5) 为 Int16/Short 整数类型。但是带有 Number(5) 的列可以保存高达 99999 的值,这比“短”整数要大很多。

所以我手动编辑了我的 .edmx 文件和我的生成类以将我的 Int16/Short 类型替换为 Int32/long

以下是有关使用 Oracle 的自定义映射类型的更多文档: http://docs.oracle.com/cd/E63277_01/win.121/e63268/entityDataTypeMapping.htm#ODPNT8303

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多