LINQ to SQL - decimal data type truncated instead of rounded

I have a decimal number DB column (6.1). When saving a record with LINQ to SQL, it truncates the value instead of rounding.

So, for example, if this parameter passed to this column in the generated LINQ to SQL query has this value ... - @ p1: input decimal (size = 0; prefix = 6; scale = 1) [222.259] the data will be displayed in DB as 222.2 instead of 222.3

Then, if I change the same column in the database to decimal (7.2) and I do not regenerate LINQ to SQL classes, saving the record using LINQ will still truncate ... - @ p1: input decimal (size = 0; prefix = 6; scale = 1) [222.259] the data will be displayed in the database as 222.20 instead of 222.26 (or 222.30)

Does anyone know if LINQ behavior is correct? Or a SqlServer provider? It doesn't behave the same way as manually recording a query in mgmt studio, so I got confused about why it truncates instead of rounding.

The following query in the mgmt studio ... UPDATE TheTable SET TheDecimalColumn = 222.259 sets the value of val to 222.3 when the column is decimal (6.1) and 222.26 when it is decimal (7.2)

thanks

+3
source share
3 answers

This is a sqlparameter, and it should do this, because fraction rounding is not standardized: there are different rounding algorithms that are used in different areas, for example, banks use different rounding standards than others.

, , , . , , .256 , , (linq to sql ), .

+6

SQL LINQ DataContext.Log? , , . , SQL. .dlinq.

PS- .. -?

0

. , LINQ/EF SQL, EntityConfiguration Entity:

namespace your.namespace.in.here
{
    class foobarEntityConfiguration : EntityTypeConfiguration<foobarEntity>
    {
        public foobarEntityConfiguration()
        {
            this.Property(x => x.foobarPropertyBeingTruncated)
                .HasPrecision(18, 5);
        }
    }
}

LINQ/EF , , , , - . / . , SQL -LINQ-, .

, LINQ 2 . , LINQ/EF , .

0

Source: https://habr.com/ru/post/1703584/


All Articles