使用LINQ和带输出参数的存储过程丢失小数精度和小数位数

时间:2022-05-06 16:59:23

I have a stored procedure that uses output parameters like this:

我有一个使用输出参数的存储过程,如下所示:

ALTER PROCEDURE [GetAmount] 
( 
@orderID [int], 
@totalcost decimal(18,2) OUTPUT 
) 
SELECT @totalcost = cost 
FROM mytable 
WHERE orderID = @orderID 

When I drag the stored procedure onto the designer, the resulting code in the designer.cs file ends up losing the precision and scale, like this:

当我将存储过程拖到设计器上时,designer.cs文件中的结果代码最终会失去精度和比例,如下所示:

[Parameter(DbType="Decimal")] ref System.Nullable<decimal> totalcost 

This is a problem, because things like product prices and order totals are being rounded up (i.e. 19.95 becomes 20).

这是一个问题,因为产品价格和订单总数之类的东西正在四舍五入(即19.95变为20)。

Now, I can manually correct the .cs file, but I'd have to remember to do it every time an update is made. Am I doing something wrong? Is there a way to change my stored procedure that would enable LINQ to automatically detect precision and scale?

现在,我可以手动更正.cs文件,但我必须记住每次更新时都要这样做。难道我做错了什么?有没有办法改变我的存储过程,使LINQ能够自动检测精度和规模?

2 个解决方案

#1


Can you use the money type rather than the decial(18,2) type?

你可以使用货币类型而不是decial(18,2)类型吗?

#2


I can manually correct the .cs file, but

我可以手动更正.cs文件,但是

Right, you need to move this code into a partial class file, edit the mapping's precision there, and drop the stored procedure from the designer.

是的,您需要将此代码移动到部分类文件中,在那里编辑映射的精度,并从设计器中删除存储过程。

This gives you a manual specification in code of the mapping to the stored procedure.

这为您提供了存储过程映射代码的手动规范。

#1


Can you use the money type rather than the decial(18,2) type?

你可以使用货币类型而不是decial(18,2)类型吗?

#2


I can manually correct the .cs file, but

我可以手动更正.cs文件,但是

Right, you need to move this code into a partial class file, edit the mapping's precision there, and drop the stored procedure from the designer.

是的,您需要将此代码移动到部分类文件中,在那里编辑映射的精度,并从设计器中删除存储过程。

This gives you a manual specification in code of the mapping to the stored procedure.

这为您提供了存储过程映射代码的手动规范。