c# - 从 C# 到 SQL Server 的精度损失

标签 c# sql-server entity-framework decimal sqldatatypes

这是我面临的一个问题,当从 C# Entity Framework 存储到 SQL Server 数据库时会导致精度损失。

  1. SQL Server 数据类型是 decimal(20, 15)
  2. 在 C# 中,属性定义为 public decimal AssignedGrossBudget { get;放; }
  3. 在 C# 中,变量 (AssignedGrossBudget) 的值是 34.09090909090909
  4. 但在 SQL Server 表中它是 34.090000000000000

有什么问题吗? (我正在使用 Entity Framework db.SaveChanges(); 和 SQLBulkCopy 将数据从 c# 存储到 SQL Server)

我想存储 34.09090909090909 而不是 34.090000000000000。

我通过直接插入表格进行了检查,它有效。

最佳答案

谢谢 Urril 和 Remus。

我在 Entity Framework 中做了如下更改:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<StationMapping>().Property(x => x.AssignedGrossBudget).HasPrecision(35, 15);

        }

对于 SQL BulkCopy,我按照 Remus 的建议添加了数据类型。

SpotLookupTable.Columns.Add(new DataColumn("GrossBudget",typeof(decimal)));

它现在正在工作并且没有损失(或可忽略不计)。

干杯

关于c# - 从 C# 到 SQL Server 的精度损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782996/

相关文章:

c# - 仅向 SQL 表插入新数据

SQL Server 2005 : How to add a column to a table at the beginning of the table?

c# - IComparer 逻辑将层次结构排序为平面列表

c# - 使用 .Take() 方法对实体的 Linq 非常慢

c# - GetHashCode 是线程安全的吗?

sql-server - 防止批量插入时锁定整个表

asp.net-mvc - 使用 EntityFramework 和映射的 ViewModels 的 ASP.NET MVC 最佳实践

c# - 带有 Entity Framework 的多对多插入的 TransactionScope 返回 TimeoutException

c# - ID为 'UpdatePanel1'的控件需要页面上有ScriptManager

c# - 运算符和泛型类