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

查看:79
本文介绍了十进制输出参数在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. 搜索文本<参数名称= b Type =数字 Mode = InOut /> 然后将其替换为< Parameter Name = b Type = numeric Mode = InOut Precision = 20 Scale = 2 />

  3. 搜索文本<参数名称= b Mode = InOut Type = Decimal /> 然后将其替换通过<参数名称= b模式= InOut类型=十进制精度= 20比例= 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天全站免登陆