实体框架代码首先截断我的小数位 [英] Entity Framework Code First truncating my decimals

查看:220
本文介绍了实体框架代码首先截断我的小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 5应用程序上使用Code First方法使用Entity Framework 6.x。在这种特殊情况下,我的模型(除其他外)包含两个名为纬度和经度的属性:

I am using Entity Framework 6.x using the Code First approach on an MVC 5 application. In this particular situation my model (among other things) contains two properties named Latitude and Longitude:

[Required, Range(-90, +90)]
public decimal Latitude { get; set; }

[Required, Range(-180, +180)]
public decimal Longitude { get; set; }

当我执行迁移时,我得到了这样的一个

And when I performed the migration I got something like this

CreateTable("ResProperty"), c => new {
        :
    Latitude = c.Decimal(nullable: false, precision: 10, scale: 8),
    Longitude = c.Decimal(nullable: false, precision: 11, scale: 8),
        :
})
... other stuff

所以纬度和经度都有8 十进制位数。前者具有2个整数(最多90个),后者具有3个整数(最多180个)。

so both latitude and longitude have 8 decimal digits. The former with 2 whole numbers (max 90) and the latter with 3 whole numbers (max 180).

执行Update-Database命令后,我的表的列显示为:

After performing the Update-Database command my table's columns are shown as:

Latitude decimal(10,8)
Longitude decimal(11,8)

对我来说似乎很好。现在在我看来,我有一个地图和Javascript代码,允许用户重新定位标记。这也很好。当标记重新定位时,纬度和经度字段填充更新的值(Javascript)具有超过12位十进制数字。这不重要AFAIK因为我的缩放是8位小数。

that seems good to me. Now In my view I have a map and Javascript code that allows the user to reposition the marker. That works fine too. When the marker is repositioned the Latitude and Longitude fields are populated with the updated value which (Javascript) has more than 12 decimal digits. That does not matter AFAIK because my scale is 8 decimals.

按下提交按钮后,调用创建或编辑POST方法,我检查模型实例和我确认模型中传递给控制器​​的实际值是正确的,它们具有足够的十进制数字(Javascript代码位置)。所以值是正确的。

After the submit button is pressed and either the Create or Edit POST method is invoked I examine the model instance and I confirmed that the actual values passed in the model to the controller are correct, they have more than enough decimal digits (those that Javascript code place). So the value is correct.

现在...问题是在执行db.SaveChanges()后,数据库被更新 - 我已经确认了一个实际的写入/ update已经发生了 - 但是在内部,EF忽略了我的实际值,并将截断的纬度/经度四舍五入到只有两个十进制数字,所以我的Latitude在DB中显示为09.500000000,所有其他十进制数字都被归零,因为似乎已经采取了四舍五入

Now... the problem being that after db.SaveChanges() is performed the database gets updated -and I have confirmed that an actual write/update has taken place- but somehow internally the EF disregards my actual values and writes truncated latitude/longitude rounded to ONLY TWO decimal digits, so my Latitude shows in the DB as 09.500000000 all other decimal digits are zeroed because a rounding seems to have taken place.

// Prior to SaveChanges()
Latitude = 9.08521879
Longitude = -79.51658792
// After SaveChanges()
Latitude = 9.08000000
Longitude = -79.51000000



<如果我给出正确的尺度和精度,为什么要舍入它,并且列的尺寸和精度也是正确的?为什么SaveChanges会改变我的价值?

Why is it rounding it if I have given the correct scale and precision and the column has the correct scale and precision as well? why is SaveChanges altering my values?

我发现这篇文章( http://weiding331.blogspot.com/2014/01/entity-framework-decimal-value.html )这是同一个问题,但我不知道如何解决这个问题(如果有的话),因为在有问题的表格迁移之后,我已经执行了几次迁移和数据添加。

I found this post (http://weiding331.blogspot.com/2014/01/entity-framework-decimal-value.html) which is the same issue but I don't know how I can fix that (if it does) because I have already performed several migrations and data additions after the table in question was "migrated".

总结


  • 模型数据类型正确(十进制)

  • 数据库迁移代码具有正确的比例/比例( lat 10/8 lon 11/8)

  • SQL数据库列具有正确的精度/标度(纬度10/8,长11/8)

  • 在模型中传递的值对于经纬度至少有八位十进制数字

  • 该值的实际写入/更新在数据库中发生,而不会发生错误,但...

  • 数据库中记录的t值他的两列被截断为两位十进制数字,
    显示另一个最低有效的十进制数字为零(0)

  • The model data type is correct (decimal)
  • The database migration code has the correct precion/scale (lat 10/8 lon 11/8)
  • The SQL database columns have the correct precision/scale (lat 10/8, long 11/8)
  • The values passed in the model have at least 8 decimal digits for both latitude and longitude
  • the actual writing/updating of the value takes place in the database without error, but...
  • The values recorded on the database for these two columns are truncated to TWO decimal digits and show the other least significant decimal digits as zero (0)

推荐答案

EF对于SqlProviderServices (SQL Server的SqlClient提供程序的实现) - TruncateDecimalsToScale 具有特殊属性。
默认值为true,因此也可以将其更改为false值。例如:

EF has a special property for SqlProviderServices (implementation for the SqlClient provider for SQL Server) - TruncateDecimalsToScale. The default value is true so maybe you can change it to false value. For example:

public class DbContextConfiguration : DbConfiguration
    {
        public DbContextConfiguration()
        {
            var now = SqlProviderServices.Instance;
            SqlProviderServices.TruncateDecimalsToScale = false;
            this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
        }
    }

    [DbConfigurationType(typeof(DbContextConfiguration))]
    public class MyContext : DbContext
    { ... }

有关更多信息: https://msdn.microsoft.com/en-us/library/system.data.entity。 sqlserver.sqlproviderservices.truncatedecimalstoscale%28v = vs.113%29.aspx

这篇关于实体框架代码首先截断我的小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆