解决 Entity Framework 6.0 decimal 类型精度问题

?  前言

本文主要解决 EF 中对于 MSSQL 数据库的 decimal 类型经度问题,经实验该问题仅在 CodeFirst 模式的情况下发生,话不多说直接看代码。

1.   假设我们有一张 Customer 数据表,主要探究:Longitude、Latitude、LonLatSum 这三个字段。

1)   结构如下:

CREATE TABLE dbo.Customer

(

Id int NOT NULL,                            --客户Id

Name nvarchar(25) NOT NULL,                 --客户名称

Address nvarchar(25) NOT NULL,              --地址

Longitude real NOT NULL,                    --经度

Latitude float NOT NULL,                    --纬度

LonLatSum decimal(18,7) NOT NULL,           --经纬度之和

CONSTRAINT PK_Customer_Id PRIMARY KEY CLUSTERED

(

Id ASC

) ON [PRIMARY]

) ON [PRIMARY];

2)   数据如下:

2.   首先,我们先使用 DB Frirst 的方式对数据更新(更新 Id 为1的数据)

1)   C# 代码如下:

using (MyTestingEntities context = new MyTestingEntities())

{

Customer entity = context.Customers.Attach(new Customer() { Id = 1 });

entity.Longitude = 123.1256789f;    //123.125679

entity.Latitude = 456.1295678d;     //456.1295678

entity.LonLatSum = (decimal)(entity.Longitude + entity.Latitude);   //579.255246816113M

context.Configuration.ValidateOnSaveEnabled = false;

result = context.SaveChanges() > 0;

}

2)   生成SQL:

exec sp_executesql N‘UPDATE [dbo].[Customer]

SET [Longitude] = @0, [Latitude] = @1, [LonLatSum] = @2

WHERE ([Id] = @3)

‘,N‘@0 real,@1 float,@2 decimal(18,7),@3 int‘,@0=123.12567901611328,@1=456.12956780000002,@2=579.2552468,@3=1

3)   执行结果:

4)   结论:

1.   Longitude:real 类型(对应 C# 中的 float 类型),进行了四舍五入,保留了4位小数。

2.   Latitude:float 类型(对应 C# 中的 double 类型),保留了7位小数。

3.   LonLatSum:decimal 类型(对应 C# 中的 decimal 类型),也保留了7位小数。

4.   OK 这是正常的。

3.   然后,我们再使用 Code Frirst 的方式对数据更新(更新 Id 为2的数据)

1)   C# 代码如下:

using (MyTestingContext context = new MyTestingContext())

{

Customer entity = context.Customer.Attach(new Customer() { Id = 2 });

entity.Longitude = 123.1256789f;    //123.125679

entity.Latitude = 456.1295678d;     //456.1295678

entity.LonLatSum = (decimal)(entity.Longitude + entity.Latitude);   //579.255246816113M

result = context.SaveChanges() > 0;

}

return result;

2)   生成SQL:

exec sp_executesql N‘UPDATE [dbo].[Customer]

SET [Longitude] = @0, [Latitude] = @1, [LonLatSum] = @2

WHERE ([Id] = @3)

‘,N‘@0 real,@1 float,@2 decimal(18,2),@3 int‘,@0=123.12567901611328,@1=456.12956780000002,@2=579.25,@3=2

3)   执行结果:

4)   结论:

1.   Longitude:与 DB First 相同。

2.   Latitude:与 DB First 相同。

3.   LonLatSum:却进行了四舍五入,只保留了两位小数,这是为什么呢?

4.   问题分析

1)   DB First 与 Code First 几乎是相同的代码,为什么 DB First 就是正常的呢,这是因为 DB First 的配置文件有这样一句:<Property Name="LonLatSum" Type="decimal" Precision="18" Scale="7" Nullable="false" />,这里明确指定了该字段精度与小数位数,生成SQL时就会按照配置去生成,例如:decimal(18,7)。

2)   而 Code First 并没有这样的配置,所以就采用了 decimal 的默认精确度(18)和小数位数(2位)的方式生成了,结果 SQL 的类型声明是这样:decimal(18,2)。

3)   搞清楚了问题,下面我们就来解决这个问题吧。

5.   解决问题

1)   创建一个 DecimalPrecisionAttribute 特性类

/// <summary>

/// 用于指定 decimal 类型的精确度与小数保留位数。

/// </summary>

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]

public class DecimalPrecisionAttribute : Attribute

{

private byte _precision;

/// <summary>

/// 精确度。

/// </summary>

public byte Precision

{

get { return _precision; }

set { _precision = value; }

}

private byte _scale;

/// <summary>

/// 小数保留位数。

/// </summary>

public byte Scale

{

get { return _scale; }

set { _scale = value; }

}

/// <summary>

/// 根据指定的精确度与小数保留位数,初始化 DecimalPrecisionAttribute 的实例。

/// </summary>

/// <param name="precision">精确度。</param>

/// <param name="scale">小数保留位数。</param>

public DecimalPrecisionAttribute(byte precision, byte scale)

{

this.Precision = precision;

this.Scale = scale;

}

}

2)   再创建一个 DecimalPrecisionAttributeConvention 类(表示 DecimalPrecisionAttribute 的一种约定)

1.   该类继承于 System.Data.Entity.ModelConfiguration.Conventions.PrimitivePropertyAttributeConfigurationConvention 类。

2.   并实现 Apply 抽象方法,通俗点说:该方法在“生成 SQL”时被调用,对于打了 DecimalPrecisionAttribute 标记的实体属性,精度将根据 configuration.HasPrecision() 方法中的设置去生成。

/// <summary>

/// 表示 DecimalPrecisionAttribute 的一种约定。

/// </summary>

public class DecimalPrecisionAttributeConvention

: PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>

{

public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)

{

if (attribute.Precision < 1 || attribute.Precision > 38)

{

throw new InvalidOperationException("Precision must be between 1 and 38.");

}

if (attribute.Scale > attribute.Precision)

{

throw new InvalidOperationException("Scale must be between 0 and the Precision value.");

}

configuration.HasPrecision(attribute.Precision, attribute.Scale);

}

}

3)   在数据上下文的 OnModelCreating() 方法将该 DecimalPrecisionAttributeConvention(约定)加入数据约定集合中。

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder);

modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());

}

4)   最后一步,将需要设置小数位数的属性打上 DecimalPrecision 标记,例如:

[DecimalPrecision(18, 7)]

public decimal LonLatSum { get; set; }

5)   好了之后再次运行代码,生成 SQL 如下:

exec sp_executesql N‘UPDATE [dbo].[Customer]

SET [Longitude] = @0, [Latitude] = @1, [LonLatSum] = @2

WHERE ([Id] = @3)

‘,N‘@0 real,@1 float,@2 decimal(18,7),@3 int‘,@0=123.12567901611328,@1=456.12956780000002,@2=579.2552468,@3=2

6)   结果如下:

7)   OK,这样就与 DB First 生成的 SQL 没什么区别了。

原文地址:https://www.cnblogs.com/abeam/p/8591875.html

时间: 2024-08-29 07:50:04

解决 Entity Framework 6.0 decimal 类型精度问题的相关文章

解决Entity Framework中DateTime类型字段异常

今天把一个使用了Entity Framework的程序从MySql迁移到SqlServer时,发现运行时报了一个异常: System.Data.SqlClient.SqlException: 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值. 在网上查找了一下,具体的错误原因是:C#中的DateTime类型比SqlServer中的datetime范围大.SqlServer的datetime有效范围是1753年1月1日到9999年12月31日,如果超出这个范

Entity Framework 5.0 Code First全面学习

目录(?)[+] 不贴图片了,太累. Code First 约定 借助 CodeFirst,可通过使用 C# 或Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code First 时基于类定义自动配置概念模型.约定是在 System.Data.Entity.ModelConfiguration.Conventions 命名空间中定义的. 可通过使用数据注释或Fluent API 进一步配置模型.优先级是通过 Fluent API 进行

Entity Framework 5.0 Code First全面学习 (转)

原文地址:感谢原文作者 http://blog.csdn.net/gentle_wolf/article/details/14004345 不贴图片了,太累. Code First 约定 借助 CodeFirst,可通过使用 C# 或Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code First 时基于类定义自动配置概念模型.约定是在 System.Data.Entity.ModelConfiguration.Convention

初次开发 ASP.NET vNext 续篇:云优化的概念、Entity Framework 7.0、目前性能还不够好

继续上一篇<开发 ASP.NET vNext 初步总结(使用Visual Studio 2014 CTP1)>之后, 关于云优化和版本控制: 我本想做一下MAC和LINUX的self-host测试,但是官方说运行环境的MONO版本至少需要3.4.1,我去年买了个表,至本文发布为止,你让我下地狱去找3.4.1吗,硬着头皮用3.4.0搞了一晚上,MAC一直停留在 httpapi.dll出错,Ubuntu Server 12.0.4 是不认其中的几个DLL包,具体哪几个也忘了,过段时间有了稳定版本再

Entity Framework 教程——Entity Framework中的实体类型

Entity Framework中的实体类型 : 在之前的章节中我们介绍过从已有的数据库中创建EDM,它包含数据库中每个表所对应的实体.在EF 5.0/6.0中,存在POCO 实体和动态代理实体两种. POCO Entity (Plain Old CLR Object): POCO类是不依赖任何框架的类型,如同其他正常的一般类型,我们称之为"Plain Old CLR Objects"(这里不知道怎么翻译,普通的CLR对象?古老的CLR对象?大概意思就是没有什么特殊的对象吧). POC

利用 POCO 解决 Entity Framework 的默认值 (Default Value) 写入问题 v4

利用 POCO 解决 Entity Framework 的默认值 (Default Value) 写入问题 经过 6 小时的实践和验证后,原本文章 po 出来的作法,只有在小弟特定的项目和 UT? 才可以执行?? ( 原因尚不明 ) 小弟重新建置相同的程序和数据库后却无法重现同样的效果. 另外,虽然 很多文章在反应是 CSDL 有更新,但 SSDL 没有更新是 Bug . 但小弟导入 StoreGeneratedPattern = Computed 后,发现若是 SSDL 也改成 Compute

Entity Framework 6.0 对枚举的支持/实体添加后会有主键反回

实验 直接上代码,看结果 实体类 [Flags] public enum FlagsEnum { Day = 1, Night = 2 } public class EntityWithEnum { public int ID { get; set; } public FlagsEnum ValidTime { get; set; } } 数据库上下文 public partial class CodeFirstModel : DbContext { public CodeFirstModel(

Entity Framework 6.0 Tutorials(1):Introduction

以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.aspx ------------------------------------------------------------------------------------------------------------- Entity Framework 6.0 Introduction: W

Entity Framework 6.0 IIS 部署出错解决方案

我自己写的webAPI + EF6.0 本地运行正常,部署到Windows Server2008中的IIS7.5中报错. 报错信息如下: "Message":"出现错误.","ExceptionMessage":"无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“System.Data.Entity.SqlServer.SqlProviderS