EF6 appears to be inconsistent in how it handles rounding when multiplying and adding integer columns with decimal values.
// CREATE TABLE MyTable (MyIntValue INT NOT NULL)
// INSERT INTO MyTable (MyIntValue) VALUES (10)
const int IntScale = 5;
const decimal DecimalScale = 5;
const decimal DecimalScale2 = 5.0m;
context.Set<MyTable>()
.Select(row => new
{
WithFloats = 0.5f + (row.MyIntValue * 5.0f), // 50.5
WithDecimals = 0.5m + (row.MyIntValue * 5.0m), // 51
WithDecimals2 = 0.5m + ((decimal)row.MyIntValue * 5.0m), // 50.5
WithDecimals3 = 0.5m + ((decimal)row.MyIntValue * IntScale), // 51
WithDecimals4 = 0.5m + ((decimal)row.MyIntValue * (decimal)IntScale) // 51
WithDecimals5 = 0.5m + ((decimal)row.MyIntValue * DecimalScale) // 51
WithDecimals6 = 0.5m + ((decimal)row.MyIntValue * DecimalScale2) // 50.5
})
.Single();
Surely this isn't the expected/correct behaviour? I would expect the WithDecimals value to be 50.5 (not 51). Am I overlooking something simple? How can I ensure that WithoutDecimals doesn't get rounded without changing the type of the other constants?
The generated SQL for WithFloats and WithDecimals (respectively):
,CAST(0.5 AS REAL) + (CAST(MyIntValue AS REAL) * CAST(5 AS REAL)) AS WithFloats
,0.5 + (CAST(MyIntValue AS DECIMAL(19,0)) * CAST(5 AS DECIMAL(18))) AS WithDecimals