简体   繁体   中英

Scale on decimal number with Entity Framework

I have a table containing a defined field called Days1 as decimal(5,2) this table is mapped in edmx file and there's a procedure to insert values to this table.

Whenever I try to insert the value 7.5 with my code

x.Days1 = 7.5;

I get the following error:

"Error converting data type numeric to decimal."

If I execute the procedure with the same value no error occurs. If I put the value 7.51 with my code:

x.Days1 = 7.51

no error appears. I think the problem is with the number of digits after dot. what do I have to do for the value 7.5 how can I send it as 7.50

Stored procedure:

ALTER PROCEDURE [dbo].[proc_EmployeeBalanceInsert]
(
    @EmployeeBalanceID char(36),
    @EmployeeID char(36),
    @LeaveRegulationID char(36),
    @AttendanceTypeID char(36),
    @BalanceOpenDate datetime,
    @DateFrom datetime,
    @DateTo datetime,
    @BalanceUpToDate datetime = NULL,
    @ValidUntilDate datetime,
    @LeaveTransactionTypeID char(1) ,
    @TransactionSign int = NULL,
    @Days1 decimal(5,2) = NULL,
    @Days2 decimal(5,2) = NULL,
    @Days3 decimal(5,2) = NULL,
    @Days4 decimal(5,2) = NULL,
    @Days5 decimal(5,2) = NULL,
    @System int = NULL,
    @CreationDate datetime = NULL,
    @BatchID char(36) = NULL
)
AS
BEGIN
    SET NUMERIC_ROUNDABORT on

    SET NOCOUNT OFF
    DECLARE @Err int

    INSERT
    INTO [EmployeeBalance]
    (
        [EmployeeBalanceID], [EmployeeID], [LeaveRegulationID],
        [AttendanceTypeID], [BalanceOpenDate], [DateFrom],
        [DateTo], [BalanceUpToDate], [ValidUntilDate],
        [LeaveTransactionTypeID], [TransactionSign], [Days1],
        [Days2], [Days3], [Days4],
        [Days5], [System], [CreationDate],
        [BatchID]
    )
    VALUES
    (
        @EmployeeBalanceID, @EmployeeID, @LeaveRegulationID,
        @AttendanceTypeID, @BalanceOpenDate, @DateFrom,
        @DateTo, @BalanceUpToDate, @ValidUntilDate,
        @LeaveTransactionTypeID, @TransactionSign, @Days1,
        @Days2, @Days3, @Days4,
        @Days5, @System, @CreationDate,
        @BatchID
    )

    SET @Err = @@Error

    RETURN @Err
END

我在保存数据之前添加以下代码解决了我的问题:

x.Days1 = Convert.ToDecimal(x.Days1.ToString("n2"));

尝试将变量设置为“true”十进制值:

x.Days1 = 7.5m; // Notice the "m" appended to the value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM