EntityFramework参数值超出范围

时间:2016-01-19 09:19:32

标签: c# asp.net sql-server asp.net-mvc entity-framework

尽管已经阅读了所有答案,但无法处理十进制和EF。 我定义了这个字段的SQLServer 2012表( Traffico ):

[traffico_out] [decimal](30, 10) NULL

如果我使用这个简单的查询进行测试:

UPDATE dbo.Traffico
SET traffico_out=55534448359.141929
WHERE id=10;

一切正常,存储小数值。

我使用Entity Framework 6 数据库优先方法部署ASP.NET MVC 4应用程序。 我从现有表格开始生成模型。

上下文

public partial class PeeringEntities : DbContext
{

  public PeeringEntities()
    : base("name=PeeringEntities")
  {
  }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    throw new UnintentionalCodeFirstException();
  }

  public virtual DbSet<Traffico> Traffico { get; set; }

}

表格实体

public partial class Traffico
{
  public int id { get; private set; }
  public Nullable<decimal> traffico_out { get; set; }
}

EDMX

<EntityType Name="Traffico">
  <Property Name="id" Type="int" StoreGeneratedPattern="Computed" Nullable="false" />
  <Property Name="traffico_out" Type="decimal" Precision="30" Scale="10" />
</EntityType>

所有代码ablove都已自动生成。

异常

当我尝试将新记录添加到&#39; Traffico &#39;表,我得到一个超出范围的错误。在我的Controller中(其中 db 是DbContext):

Traffico _new_item = new Traffico();
_new_item.traffico_out = 5283148511.91876M;
db.Traffico.Add(_new_item);
db.SaveChanges();

在SaveChanges时,我收到了一个DbUpdateException:

InnerException: An error occurred while updating the entries. See the inner exception for details.
InnerException -> InnerException: {"Parameter value '5283148511,91876' is out of range."}

我做错了什么?

修改 查看我的输出目录,我发现了三个从我的EDMX生成的文件:

  1. .csdl
  2. .msl
  3. .ssdl
  4. 尽管有edmx的精确度和规模,我在.ssdl中找到了:

    <Property Name="traffico_out" Type="decimal" Precision="12" Scale="5" />
    

    现在我真的很困惑:我怎么能改变这个?

1 个答案:

答案 0 :(得分:0)

55534448359.141929大于Decimal(12,5)中可存储的值。

12(精度)是数字中允许的最大位数,因此为999999999999 并且scale是小数点后的最大位数。因此,9999999.99999是您可以存储在十进制(12,5)

中的最高小数