ef4 poco 双/浮点 dbtype 映射
我有一个现有的数据库,其中有几个浮点/双精度字段,我选择使用 EF4.1 作为我的 DAL/ORM,但在将数据读取/保存到他的字段类型时遇到问题。没有模型,只是使用模型构建器来配置实体。
首先存在一个舍入问题,我认为这是浮点数学的典型问题,因此因为所有精度都已设置为 2 位小数,所以我切换到在 C# (EF) 端使用小数类型,但现在当我尝试获取一个实体,我收到一个异常,说我遇到了麻烦。
“YYY”的“xxxkg”属性无法设置为“Double”值。 您必须将此属性设置为“Decimal”类型的非空值。
xxxkg
在数据库上是float null
,在具体情况下值为10
,在类中它是
public decimal? xxxkg { get; set; }
问题是如何使用 EF4 (poco) 处理 dbtype float/double 变量?
I have an exiting database with couple of float/double fields, I chose to use EF4.1 for my DAL/ORM, but I am having problems when reading/saving data to his type of fields. There is no model, just using model builder to configure the entities.
First there was a rounding problem, I think typical of floating point math, so because all of the precision were already set as 2 decimals, I switched to using decimal type on the c# (EF) side, but now when I am trying to get an entity, I get an exception saying I am in trouble.
The 'xxxkg' property on 'YYY' could not be set to a 'Double' value.
You must set this property to a non-null value of type 'Decimal'.
xxxkg
is float null
on the database, and the value is 10
in the specific case, and in the class it is
public decimal? xxxkg { get; set; }
The question is how to deal with dbtype float/double variables with EF4 (poco)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果数据库中的属性是双精度的,则不能将属性定义为十进制。 EF 不进行类型转换(并且不允许您定义自己的类型转换),并且无法将双精度值分配给十进制变量,因此从读取记录填充您的类将会失败。您必须使用双倍。
You cannot define your property as decimal if it is double in database. EF does not type conversion (and doesn't allow you defining your own) and double value cannot be assigned to decimal variable so populating your class from read record will fail. You must use double.