EF5.0 中的十进制输出参数四舍五入为整数 [英] Decimal output parameter rounded to integer in EF5.0

查看:27
本文介绍了EF5.0 中的十进制输出参数四舍五入为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用类似于以下返回十进制输出的现有数据库存储过程,

We are using existing database stored procedure similar to the one below that returns decimal output,

CREATE PROCEDURE spTest(@a int, @b decimal(18,2) output)
as
BEGIN
   SELECT @b=23.22
   SELECT * FROM <TABLE> where id = @a
END

当我在 C# 应用程序(下面的代码)中调用存储过程时,我得到输出参数的结果为 23 而不是 23.22

When I call the stored procedure in in C# app (code below) I get the result for the output parameter as 23 instead of 23.22

ObjectParameter b = new ObjectParameter("b", typeof(System.Decimal))
var result = myentities.context.spTest(1234, b)

这与 Imre Horvath 发布的完全相同的问题 (http://social.msdn.microsoft.com/Forums/en-US/14bdde82-c084-44dd-ad83-c1305cb966d2/decimal-output-parameter-rounded-to-integer) 但不同之处在于我们使用的是 SQL Server 2008 和实体框架 5.0.在阅读了他帖子中的建议后,我在 xml 编辑器中打开了 edmx 文件,并注意到输出参数 @b 如下所示,

This exactly the same issue posted by Imre Horvath (http://social.msdn.microsoft.com/Forums/en-US/14bdde82-c084-44dd-ad83-c1305cb966d2/decimal-output-parameter-rounded-to-integer) but the difference is we are using SQL Server 2008 and entity framework 5.0. After reading the suggestion from his post I have opened the edmx file in xml editor and noticed the following for the output parameter @b as below,

<Parameter Name="b" Type="decimal" Mode="InOut"/>;

我改成

<Parameter Name="b" Type="decimal" Mode="InOut" Precision="18" Scale="2"/>;

并运行应用程序,我得到了预期的结果 (23.22)

and run the application and I got the result as expected (23.22)

这是一种变通方法,但不是解决方案,因为您知道,当我们在实体框架设计器中更新存储过程时,更改将会丢失.在我们的数据库中,我们有很多以十进制(18,2)作为输出参数的存储过程.我想知道这在实体框架 5.0 中是否仍然是一个问题.非常感谢您的帮助.

This is a work around but not a solution as you know that changes will be lost when we update the stored procedure in the entity framework designer. In our database we have lots of stored procedure that has decimal(18,2) as output parameter. I'm wondering whether this still an issue in entity framework 5.0. Your help will be much appreciated.

库马尔

推荐答案

我和你有同样的问题,参考这篇文章后我已经解决了 http://tiku.io/questions/1120572/decimal-output-parameter-rounded-to-integer-in-ef5-0

I got the same issue with you and I have resolved it after I referenced from this article http://tiku.io/questions/1120572/decimal-output-parameter-rounded-to-integer-in-ef5-0

有 3 个步骤可以解决此问题:

There is 3 steps to resolve this issue:

  1. 右键单击 edmx 文件 => 打开方式... => XML(文本)编辑器.
  2. 搜索文本 <Parameter Name="b" Type="numeric" Mode="InOut"/> 然后将其替换为 <Parameter Name="b" Type="numeric" Mode="InOut" Precision="20" Scale="2"/>
  3. 搜索文本 <Parameter Name="b" Mode="InOut" Type="Decimal"/> 然后将其替换为 <Parameter Name="b" Mode="InOut" Type="Decimal" Precision="20" Scale="2"/>
  1. Right click on edmx file => Open with... => XML (text) Editor.
  2. search text <Parameter Name="b" Type="numeric" Mode="InOut" /> then replace it by <Parameter Name="b" Type="numeric" Mode="InOut" Precision="20" Scale="2" />
  3. search text <Parameter Name="b" Mode="InOut" Type="Decimal" /> then replace it by <Parameter Name="b" Mode="InOut" Type="Decimal" Precision="20" Scale="2" />

希望对您有所帮助!

这篇关于EF5.0 中的十进制输出参数四舍五入为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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